nginx No 'Access-Control-Allow-Origin' header is present on the requested resource
时间: 2023-08-26 18:18:14 浏览: 160
回答: 当在访问不同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 ]
阅读全文