在对博客进行静态文件剥离的时候,遇到主站调用fonts字体库标签的地方都显示为空。通过开发者工具查看,居然一大串的报错信息Access-Control-Allow-Origin 。
浏览器报错原文:Font from origin 'http://static.shuchengxian.com' has been blocked from loading by Cross-Origin Resource Sharing policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://www.shuchengxian.com' is therefore not allowed access.
解释:从起源的http://static.shuchengxian.com字体被阻止通过跨域资源共享策略加载:没有访问控制允许源头是目前对请求的资源。起源http://www.shuchengxian.com”因此不允许访问。
解决方法:
通过Nginx模块HttpHeadersModule来添加Access-Control-Allow-Origin允许的地址。
修改子域名http://static.shuchengxian.com的Nginx服务器域名配置文件即可,添加以下代码:location ~ .*\.(eof|ttf|ttc|otf|eof|woff|woff2|svg)(.*){
add_header Access-Control-Allow-Origin http://www.shuchengxian.com;
}
当然你也可以将指定的域名改成(*),这样所有的域名只要加载你的字体库文件都是可以使用的。我这里处于安全的考虑,暂时只是配置我需要的域名。