nginx http強制跳轉https 配置相關信息

 跳轉方式有兩種

1、rewrite 方式

rewrite ^(.*) https://$server_name$1 permanent;

2、return方式

return 301 https://$server_name$request_uri;

兩種方式都是添加在默認監聽端口中,比如80端口下方,當訪問到80端口則被重定向到https中


 

配置如下:

worker_processes  1;

error_log  logs/error.log info;

events {
    worker_connections  1024;
}


http {
	include       mime.types;
    #log_format  main  '$remote_addr - $remote_user [$time_local] $request '  
    #                  '"$status" $body_bytes_sent "$http_referer" '  
    #                  '"$http_user_agent" "$http_x_forwarded_for"';  
	  
    keepalive_timeout  1000;    
    proxy_read_timeout 1200;  
    gzip  on;  
    gzip_min_length  1024;  
    gzip_buffers     4 8k;  
    gzip_types       text/* application/javascript application/x-javascript text/css text/plain;  
    gzip_http_version 1.0;  
    gzip_comp_level  9;  
    gzip_proxied     any;  
    gzip_vary        on;  
	  
    #output_buffers   1 32k;  
    #postpone_output  1460;  
	  
    client_header_timeout   2048m;  
    client_body_timeout     2048m;  
    send_timeout            2048m;  
    sendfile                on;  
    tcp_nopush              on;  
    tcp_nodelay             off;  
	  
    client_header_buffer_size 256k;  
    large_client_header_buffers 2 256k;  
    server_names_hash_bucket_size 256;  
	  
    charset UTF-8;  
    client_max_body_size 2048M;
	
	upstream local_host {    
        server 127.0.0.1:8080 weight=1;
    }  
    server {
		listen       80;
        server_name  XXXX;

        #charset koi8-r;

		#rewrite ^(.*) https://$server_name$1 permanent;
		return 301 https://$server_name$request_uri;
       

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
		
    }
    

	# HTTPS server
    #
    server {
        listen       443 ssl;
        server_name  XXXX;

        ssl_certificate     XXX.pem;
        ssl_certificate_key  XXXXX.key;

        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;

        # ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_ciphers  ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
		ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_prefer_server_ciphers  on;

        location / {
            root   html;
            index  index.html index.htm;
        }

        location /xx {
            proxy_pass http://local_host;
			proxy_redirect          off;
			proxy_set_header Host $host;
			proxy_set_header X-Real-IP $remote_addr;
			proxy_set_header X-Real-PORT $remote_port;
			proxy_set_header X-Forwarded-For $scheme;
			proxy_set_header X-Forwarded-Ssl on;
        }

	
    }
}

在配置好後,正常瀏覽器訪問可以正常跳轉https,但是出現頁面調用後臺接口還是默認http的時候還是會報

最後通過添加header 頭信息解決了,最後做個記錄。

proxy_set_header X-Forwarded-Ssl on;

 

發佈了7 篇原創文章 · 獲贊 11 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章