欢迎来到代码驿站!

Linux

当前位置:首页 > 服务器 > Linux

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

时间:2021-08-09 08:51:07|栏目:Linux|点击:

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.html 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静态资源允许跨域访问解决方法

上一篇:Linux后台运行Python程序的几种方法讲解

栏    目:Linux

下一篇:Apache 获取真实ip的配置的实现方法

本文标题:解决nginx/apache静态资源跨域访问问题详解

本文地址:http://www.codeinn.net/misctech/165398.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有