Nginx 配置实现 CORS

在开发的时候,我们经常会遇到跨域问题,我们通常的解决方案是使用CORS或是JSONP来解决,但是更常用的解决方案便是CORS了,因为JSONP只支持GET请求。关于CORS的相关知识,请移步阮一峰的跨域资源共享 CORS 详解这篇文章。

如果浏览器控制台出现跨域相关的报错时,一般是后端没有允许跨域,所以需要后端服务器在代码层面设置下。当然有的时候后端并不想对代码进行改动,这个时候我们也可以直接在代理层nginx这里进行一些相关配置 比如我们要允许/api/v1下的相关接口支持跨域,我们可以这样编写nginx配置

location ^~ /api/v1 {

	add_header 'Access-Control-Allow-Origin' "$http_origin"; 
	add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS'; 
	add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type    '; 
	add_header 'Access-Control-Allow-Credentials' 'true'; 
	if ($request_method = 'OPTIONS') { 
		add_header 'Access-Control-Allow-Origin' "$http_origin"; 
		add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS'; 
		add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type    '; 
		add_header 'Access-Control-Allow-Credentials' 'true'; 
		add_header 'Access-Control-Max-Age' 1728000; # 20 天 
		add_header 'Content-Type' 'text/html charset=UTF-8'; 
		add_header 'Content-Length' 0; 
		return 200; 
	} 
    # 这下面是要被代理的后端服务器,它们就不需要修改代码来支持跨域了
	proxy_pass http://127.0.0.1:8085; 
	proxy_set_header Host $host; 
	proxy_redirect off; 
	proxy_set_header X-Real-IP $remote_addr; 
	proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
	proxy_connect_timeout 60; 
	proxy_read_timeout 60; 
	proxy_send_timeout 60; 

}
复制代码

转载于:https://juejin.im/post/5aa770415188255587233832

要在Nginx配置CORS(跨源资源共享),您需要编辑Nginx配置文件并添加相关配置。 首先,打开Nginx配置文件。默认情况下,该文件通常位于以下位置之一: - /etc/nginx/nginx.conf - /etc/nginx/conf.d/default.conf 找到您希望启用CORS的服务器块或位置块,并在其中添加以下配置: ``` location / { # 允许的来源(域名) if ($request_method = 'OPTIONS') { add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; add_header 'Access-Control-Max-Age' 1728000; add_header 'Content-Type' 'text/plain charset=UTF-8'; add_header 'Content-Length' 0; return 204; } if ($request_method = 'POST') { 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'; add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range'; } if ($request_method = 'GET') { 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'; add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range'; } # 其他配置... } ``` 在上面的配置中,我们允许所有来源('*')跨域访问。您可以根据需要更改为特定的域名。 配置完成后,保存文件并重新加载Nginx配置,以使更改生效。您可以使用以下命令重新加载Nginx: ``` sudo service nginx reload ``` 现在,您的Nginx服务器应该已经启用了CORS,并允许跨域访问。请确保您的应用程序也正确处理CORS 头部。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值