文:毛毛,首发自:maomao.ink,转载请保留此行。
刚刚为网站添加了www解析:www.maomao.ink 添加完之后打开网址,发现我的iconfont图标都不见了,变成了小框框!
No 'Access-Control-Allow-Origin' header is present on the requested resource.
博友告诉我,报这个错是因为跨域访问了,需要修改配置。
我用的是Nginx
解析服务,所以需要修改nginx.conf
文件配置。
location / {
add_header 'Access-Control-Allow-Origin' $http_origin;
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,web-token,app-token,Authorization,Accept,Origin,Keep-Alive,User-Agent,X-Mx-ReqToken,X-Data-Type,X-Auth-Token,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain; charset=utf-8';
add_header 'Content-Length' 0;
return 204;
}
}
如果你的服务器是linux系统,需要用以下命令进行重启:
/opt/openresty/nginx/sbin/nginx -s reload
重启之后,刷新一下网页,丢失的资源就都回来了~
注:如果刷新无效,试试清除浏览器缓存再刷新一次
参考资料:https://blog.csdn.net/envon123/article/details/83270277
我是谁?