環境:
阿里雲兩臺ECS,負載均衡,nginx,tomcathtml
後端nginx:
配置server端口爲 :8081,不用配置SSL,將SSL證書交給SLB處理。前端
SLB配置
配置兩個監聽,分別是:
前端80端口,後端服務81端口
前端443端口監聽後端80端口
以下圖:nginx
#nginx負載 upstream duweixin.net{ server localhost:8081; server 10.17.232.2:8087;#另外一臺負載 } server { listen 80; root /data/www/h5/; index index.html index.htm; server_name duweixin.net www.duweixin.net; location / { proxy_pass http://duweixin.net; proxy_set_header X-Forwarded-For $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $host; proxy_redirect off; } } #前端若是輸入端是 http://duweixin.net,81端口,強制跳轉到 https://duweixin.net server { listen 81; return 301 https://duweixin.net $request_uri; }
總結後端
1.若是是https的請求直接由443端口處理,SSL數據加密的任務交給SLB處理。
2.若是是http的請求從SLB的80端口轉至後端的81端口,由81端口重寫成https請求。tomcat
因此無論用戶用http仍是https最終的請求都是https的請求。
關鍵的一點是要讓SLB的IP可以訪問你後端nginx的端口,個人是81,不然SLB會檢測端口異常。負載均衡
補充:
最近發現還有另外一種特別簡單的辦法阿里雲
使用阿里雲CDN加速域名能夠設置,以下圖加密