nginx如何解决No 'Access-Control-Allow-Origin' header is present on the requested resource.
时间: 2023-11-06 09:03:05 浏览: 155
可以通过在nginx配置中添加Access-Control-Allow-Origin头部来解决"No 'Access-Control-Allow-Origin' header is present on the requested resource"的问题。根据提供的引用内容,可以使用以下两种方法来添加头部:
1. 在nginx配置中添加以下语句:add_header Access-Control-Allow-Origin *;
这将允许所有来源的请求访问资源。
2. 如果第一种方法不起作用,可以尝试添加以下语句:add_header Access-Control-Allow-Origin $http_origin;
这将允许特定来源的请求访问资源。
请注意,在使用这些方法之前,确保已经安装并正确配置了nginx,并且在修改配置文件后重新加载nginx。
相关问题
nginx 解决No 'Access-Control-Allow-Origin' header is present on the requested resource.
在nginx中解决"No 'Access-Control-Allow-Origin' header is present on the requested resource"错误,可以通过在nginx的配置文件中添加跨域的数据来实现。具体步骤如下:
1. 打开nginx的配置文件,通常位于/etc/nginx/nginx.conf或者/etc/nginx/conf.d/default.conf。
2. 在配置文件中找到对应的请求类型的location块,比如静态文件的location块。
3. 在该location块中添加以下代码来设置跨域的数据:
```shell
location /static {
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods *;
add_header Access-Control-Allow-Headers *; root /path/to/static/files;
}
```
其中,`/static`是请求的路径,`/path/to/static/files`是静态文件的根目录。
4. 保存配置文件并重启nginx服务,使配置生效。
这样,当请求静态文件时,nginx会添加相应的跨域头信息,解决"No 'Access-Control-Allow-Origin' header is present on the requested resource"错误。
nginx No 'Access-Control-Allow-Origin' header is present on the requested resource
回答: 当在访问不同ip的nginx服务器时,出现"No 'Access-Control-Allow-Origin' header is present on the requested resource"的错误时,这是由于被请求的资源没有设置 'Access-Control-Allow-Origin' 导致的。为了解决这个问题,可以在nginx配置文件中添加以下代码:
location / {
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
}
或者可以添加以下配置:
location / {
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept";
add_header Access-Control-Allow-Methods "GET, POST, OPTIONS";
}
然后重启Nginx服务,即可解决该问题。这样配置会添加响应的header参数,允许跨域访问。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [使用nginx解决Access-Control-Allow-Origin问题](https://blog.csdn.net/rgrgrwfe/article/details/123081089)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *2* [配置Nginx后出现No ‘Access-Control-Allow-Origin‘ header is present on the requested resource](https://blog.csdn.net/weixin_45391817/article/details/115143225)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *3* [Nginx配置跨域请求Access-Control-Allow-Origin * 详解](https://blog.csdn.net/qq_50523945/article/details/131072783)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
[ .reference_list ]
阅读全文