關於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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章