nginx的https和http共存反向代理配置
一、设置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端口。
申请证书,FreeSSL有免费的证书可供使用,会提供一个key文件和一个pem文件。将证书放在conf目录下。
如果是整站https而不允许http的情况下可如下设置:
server { listen 80; listen 443; ssl on; ssl_certificate 214****.pem; 证书文件1 ssl_certificate_key 214****.key; 证书文件2,如果放到其他目录,这里加上目录名,如cert/c.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的资源引用了。