nginx的https和http共存反向代理配置

石曼迪發表於2017-10-26

一、設定http反向代理:

upstream ly.com {
server 192.168.1.100:88;
server 192.168.1.101:88;
}

upstream home.ly.com {
server 192.168.1.100:90;
server 192.168.1.101:90;
}

 對應增加:

server {
listen 80;
server_name ly.com;
location / {
proxy_pass http://ly.com;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Cookie $http_cookie;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}

server {
listen 80;
server_name home.ly.com;
location / {
proxy_pass http://home.ly.com;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Cookie $http_cookie;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}

二、配置ssl:

先開啟nginx所在伺服器443埠。

申請證書,阿里雲有免費的證書可供使用,會提供一個key檔案和一個pem檔案。將證書放在conf目錄下。

如果是整站https而不允許http的情況下可如下設定:

server {
listen 80;
listen 443;
ssl on;
ssl_certificate 214****.pem;
ssl_certificate_key 214****.key;
ssl_session_timeout 5m;
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;

如果要共存只需將前三行改成:

server {
listen 80;
listen 443 ssl;
#ssl on;

意思是隻有443埠是https方式80埠還是http方式訪問。

另開啟了ssl,網站就不能有非https的資源引用了。

相關文章