Nginx | Nginx之跨域配置(CORS)

1. 注意

  • 服务端口不能与代理端口一致 端口会冲突
  • 服务本身做了跨域处理 nginx 就不用在处理跨域了

2. CORS

什么是CORS ? CORS是一个W3C的标准,全称是跨域资源共享(Cross-origin resource sharing). 他允许浏览器向跨源服务器,发出XMLHttpRequest请求,从而客服了AJAX只能同源使用的限制.
当前几乎所有的浏览器都可以通过CORS的协议支持AJAX跨域调用.
简单来说就是跨域的目标服务器要返回的一系列的Headers,通过这些Headers来通知是否同意跨域.

3. Nginx通过CROS 实现跨域

#设置Origin:表示服务器可以接受的请求 * 代表所有请求
add_header Access-Control-Allow-Origin 
#设置跨域请求是否允许发送Cookie,true 支持 false不支持
add_header Access-Control-Allow-Credentials
#设置跨域支持的请求类型 GET,POST,PUT,DELETE,OPTIONS
add_header Access-Control-Allow-Methods 
#设置跨域请求允许的Headers  头信息字段,以逗号分割的字符串
add_header Access-Control-Allow-Headers  

4. 配置信息 nginx.conf 中

  • 任意跨源请求都支持
任意跨源请求都支持
 server {
        listen      8080;
        server_name  localhost;
        
		#*星号代表任意跨源请求都支持
        add_header Access-Control-Allow-Origin '*';  
        add_header Access-Control-Allow-Credentials "true";
		add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
		add_header Access-Control-Allow-Headers  'token,DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,XRequested-With';
		if ($request_method = 'OPTIONS') {
			return 200;
		}
	
	location /appmains {
            proxy_pass http://appmainsserver;
        }

    }
  • 多个请求地址白名单配置
多个请求地址白名单配置
server {
        listen      8080;
        server_name  localhost;

		#设置变量 $cors_origin
        set $cors_origin "";
		#请求地址匹配格式  用来控制http请求地址 设置跨域白名单
        if ($http_origin ~ "https:www.baidu.com") {
            set $cors_origin $http_origin;              
        }	
		if ($http_origin ~ "www.test.com") {
            set $cors_origin $http_origin;              
        }
		if ($http_origin ~ "www.test1.com") {
            set $cors_origin $http_origin;              
        }		
        add_header Access-Control-Allow-Origin '$cors_origin';  
        add_header Access-Control-Allow-Credentials "true";
		add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
		add_header Access-Control-Allow-Headers  'token,DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,XRequested-With';
		if ($request_method = 'OPTIONS') {
			return 200;
		}
	
	location /appmains {
            proxy_pass http://appmainsserver;
        }

    }
  • 指定格式的请求地址跨域
指定格式的请求地址跨域
 server {
        listen      8080;
        server_name  localhost;

		#设置变量 $cors_origin
        set $cors_origin "";
		#请求地址匹配格式  用来控制http请求地址 设置跨域白名单
        if ($http_origin ~ "http://(.*).baidu.com$") {
            set $cors_origin $http_origin;              
        }	

        add_header Access-Control-Allow-Origin '$cors_origin';  
        add_header Access-Control-Allow-Credentials "true";
		add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
		add_header Access-Control-Allow-Headers  'token,DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,XRequested-With';
		if ($request_method = 'OPTIONS') {
			return 200;
		}
	
	location /appmains {
            proxy_pass http://appmainsserver;
        }

    }
  • 11
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
要在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 头部。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一介草民丶

谢谢老板的一分钱

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值