Access-Control-Allow-Origin nginx
时间: 2023-09-30 11:11:06 浏览: 155
Access-Control-Allow-Origin是一个HTTP响应头,用于指定允许访问资源的源(Origin)。在Nginx中配置Access-Control-Allow-Origin可以实现跨域访问。该配置参数的值可以设置为具体的源,也可以设置为通配符"*",表示接受所有的请求源(Origin)。
要在Nginx的配置文件中配置Access-Control-Allow-Origin,可以在nginx.conf文件中的路由部分添加以下代码:
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';
}
这样配置后,Nginx服务器就可以接受所有跨域的请求,并允许客户端访问资源。另外,还可以通过配置Access-Control-Allow-Methods和Access-Control-Allow-Headers头来指定允许的请求方法和请求头。如果请求方法为OPTIONS,可以设置返回状态码204,表示请求成功,但没有响应内容。
通过以上配置,可以解决Nginx服务器在访问不同IP时报错"No 'Access-Control-Allow-Origin' header is present on the requested resource"的问题。<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^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
- *2* *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^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文