Nginx跨域

该博客介绍了如何在Nginx服务器上配置跨域策略,通过在nginx.conf中设置upstream和server块,实现对不同端口应用的反向代理,并通过添加特定HTTP头来允许跨域请求。配置包括允许凭证、指定源、处理预检请求等关键步骤。

Nginx跨域

在nginx.conf中配置

upstream pload {
	#默认的 策略--轮询策略 RR策略
	#权重策略 weight=10;
	#ip_hash策略 解决sessin共享问题
		#ip_hash;
		server localhost:8080 ;#weight=9
		#server localhost:8081 ;#weight=1
	}
	

	#反向代理服务器
    server {
		#监听的端口号
        listen       80;
		#服务器的名称
        server_name  localhost;

		add_header Access-Control-Allow-Credentials true;
        add_header Access-Control-Allow-Origin $http_origin;
        
        location / {
		        if ($request_method = 'OPTIONS') {
                add_header Access-Control-Allow-Origin $http_origin;
                add_header Access-Control-Allow-Methods $http_access_control_request_method;
                add_header Access-Control-Allow-Credentials true;
                add_header Access-Control-Allow-Headers $http_access_control_request_headers;
                add_header Access-Control-Max-Age 1728000;
                return 204;
             }
			
			#转发的服务器的ip地址
			proxy_pass http://pload;
        }
       }
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值