Nginx經常被用於終結SSL連接,可能是因爲上游服務器不能夠使用SSL。要使用SSL就需要在編譯安裝時在Nginx的二進制文件中添加--with_http_ssl_module模塊,並且要安裝ssl證書和祕鑰。
以下示例代碼表示對客戶端和反向代理之間的流量進行加密,主要應該在內部網絡建設安全性很高的情況下。
server {
listen 443 default ssl;
server_name www.test.com;
ssl_prefer_server_ciphers on;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_certificate conf.d/cert/server.crt;
ssl_certificate_key conf.d/cert/server.key;
ssl_session_timeout 5m;
ssl_session_cache shared:WEB:10m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
location / {
proxy_http_version 1.1;
proxy_set_header X-FORWARDED-PROTO https; #使上游服務器的應用程序知道原始請求使用了https
proxy_set_header Host $host;
proxy_pass http://upstream;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
# http重定向到https
server {
listen 80;
server_name www.test.com;
rewrite ^(.*)$ https://$host$1 permanent;
}