Nginx如何配置跨域(多个域名)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/moxiaomomo/article/details/82970004

假设需要允许来源为localhost.*.example.com下所有二级域名的访问,在nginx中只需要类似这样配置即可:

    location / {
		set $match "";
		# 支持http及https
		if ($http_origin ~* 'https?://(localhost|.*\.example\.com)') {
		set $match "true";
		}
		
		if ($match = "true") {
			add_header Access-Control-Allow-Origin "$http_origin";
			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-Allow-Methods GET,POST,OPTIONS,DELETE;
			add_header Access-Control-Allow-Credentials true;
		}
		# 处理OPTIONS请求
		if ($request_method = 'OPTIONS') {
			return 204;
		}
	}

当然通过正则也可以详细指定若干个允许访问的域名来源。

猜你喜欢

转载自blog.csdn.net/moxiaomomo/article/details/82970004
今日推荐