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

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静态资源允许跨域访问解决方法
posted @   Mr.peter  阅读(1629)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
历史上的今天:
2018-10-18 Node.js
2018-10-18 React&Vue
点击右上角即可分享
微信分享提示