关于nginx如何配置多域名【http或https跨域】跨域【注意:*通配符所有域名跨域有时候不会生效,建议使用指定多域名跨域配置】

重点nginx跨域参考:https://www.cnblogs.com/gaoyuechen/p/12880939.html

 

务必注意:add_header Access-Control-Allow-Origin *; 通配符配置所有的域名跨域在nginx会无效,多次测试依然如此,建议使用指定多域名跨域!

 

【我跨域成功的配置】指定多域名配置跨域:

		#使用正则表达式配置多个域名跨域信息【nginx配置*全部域名跨域不安全,也不建议使用,其次本人配置全部跨域一直未成功,具体原因还未知...】
				set $cors_origin "";
				if ($http_origin ~* "^https://aaaa.org$") {
						set $cors_origin $http_origin;
				}
				if ($http_origin ~* "^https://www.aaaa.org$") {
						set $cors_origin $http_origin;
				}
				# http或https跨域必须配置的三条记录!
				add_header Access-Control-Allow-Origin $cors_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';
				
# nformation on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user root;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# 是否设置后台运行默认是on后台运行!:
daemon on;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
		log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

		access_log  /var/log/nginx/access.log  main;

		sendfile            on;
		tcp_nopush          on;
		tcp_nodelay         on;
		keepalive_timeout   65;
		types_hash_max_size 2048;

		include             /etc/nginx/mime.types;
		default_type        application/octet-stream;

		# Load modular configuration files from the /etc/nginx/conf.d directory.
		# See http://nginx.org/en/docs/ngx_core_module.html#include
		# for more information.

		include /etc/nginx/conf.d/*.conf;
	


		server {

				listen 80;
				#监听443端口(必须)
				listen 443 ssl;
				
				server_name aaaa.org www.aaaa.org;
				index index.html index.htm;
				root /root/www/t.radarleb.org;

				#引用证书(必须,放在conf/ssl目录下可以用相对路径,其他位置用绝对路径)
				ssl_certificate     ssl/aaaa.org.crt;
				ssl_certificate_key ssl/aaaa.org.key;

				#协议优化(可选,优化https协议,增加安全性)
				ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
				ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
				ssl_prefer_server_ciphers on;
				ssl_session_cache shared:SSL:10m;
				ssl_session_timeout 10m;

				#自动跳转到HTTPS(可选,和下面的部分域名跳转不能同时使用)
				if ($server_port = 80){
						rewrite ^(/.*)$ https://$host$1 permanent;
				}
				
				
				#使用正则表达式配置多个域名跨域信息【nginx配置*全部域名跨域不安全,也不建议使用,其次本人配置全部跨域一直未成功,具体原因还未知...】
				set $cors_origin "";
				if ($http_origin ~* "^https://aaaa.org$") {
						set $cors_origin $http_origin;
				}
				if ($http_origin ~* "^https://www.aaaa.org$") {
						set $cors_origin $http_origin;
				}
				# http或https跨域必须配置的三条记录!
				add_header Access-Control-Allow-Origin $cors_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';
			

				error_page 404 /404.html;
				location = /usr/share/nginx/html/40x.html {
				}

				error_page 500 502 503 504 /50x.html;
				location = /usr/share/nginx/html/50x.html {
					}
		}
}

 

普通参考:https://www.cnblogs.com/iLoveMyD/p/10276359.html

普通参考:https://www.cnblogs.com/yuanwanli/p/12670838.html

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值