当前位置: 移动技术网 > 网络运营>服务器>nginx > Nginx实现跨域使用字体文件的配置详解

Nginx实现跨域使用字体文件的配置详解

2019年04月18日  | 移动技术网网络运营  | 我要评论

庆阳热线,逆天修真诀,金华职业技术教务网

本文主要给大家介绍了关于nginx跨域使用字体文件的相关内容,分享出来供大家参考学习,下面来一起看看详细的介绍:

问题描述

今天在使用子域名访问根域名的css时,发现字体无法显示,在确保css和font字体的路径加载无问题后,基本确定是因为跨域的问题。

通过nginx模块http_headers_module来添加access-control-allow-origin允许的地址

console报错信息

ccess to font at 'http://w001.static.akhack.com/css/kohinoorbangla-regular.ttf' from origin 'http://carey.akhack.com' has been blocked by cors policy: no 'access-control-allow-origin' header is present on the requested resource. origin 'http://carey.akhack.com' is therefore not allowed access.

解决方法

location ~* \.(eot|ttf|woff|svg|otf)$ {
  add_header access-control-allow-origin http://carey.akhack.com;
  add_header access-control-allow-headers x-requested-with;
  add_header access-control-allow-methods get,post,options;
}

注意

add_header access-control-allow-origin http://carey.akhack.com;
//只允许单域名或者 ‘*' 。不推荐 ‘*' ,会导致安全问题

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对移动技术网的支持。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网