当前位置: 移动技术网 > 网络运营>服务器>Linux > 解决nginx/apache静态资源跨域访问问题详解

解决nginx/apache静态资源跨域访问问题详解

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

1. apache静态资源跨域访问

找到apache配置文件httpd.conf

找到这行

#loadmodule headers_module modules/mod_headers.so

把#注释符去掉

loadmodule headers_module modules/mod_headers.so

目的是开启apache头信息自定义模块

在独立主机配置文件中新增header

header set access-control-allow-origin *

例如:

<virtualhost *:88>
 serveradmin admin@example.com
 documentroot "****************"
 servername www.jb51.com
 header set access-control-allow-origin *

 errorlog "***********"
 customlog "****************************" common
<directory "**************">
 setoutputfilter deflate
 options followsymlinks execcgi
 require all granted
 allowoverride all
 order allow,deny
 allow from all
 directoryindex  index.php
</directory>
</virtualhost>
apachecopy

意思是对这个域名的资源进行访问时,添加一个头信息

重启apache

service httpd restart

2. nginx静态资源允许跨域访问

同理 找到相应域名配置文件

在server模块中添加配置:

add_header ‘access-control-allow-origin' ‘*';

例:

server {
    listen    80;
    add_header 'access-control-allow-origin' '*';
    location /roboto/ {
      root  /home/images;
      autoindex on;
    }
  }

nginx重载

./nginx -s reload

通过以上方法配置完后,再次跨域访问静态资源就没有问题了

以上既是nginx/apache静态资源允许跨域访问解决方法

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网