1. 程式人生 > >nginx配置https【轉】

nginx配置https【轉】

nginx要實現ssl,在編譯時要新增--with-http_ssl_module,如:
./configure --with-http_ssl_module

#cd /usr/local/nginx/conf
#mkdir ssl
#cd ssl
生成一個私有key
# openssl genrsa -des3 -out aoshiwei.com.key 1024
提示輸入密碼
生成CSR(Certificate Signing Request)檔案:
# openssl req -new -key aoshiwei.com.key -out aoshiwei.com.csr
填寫證書內容,組織機構、域名等,Common Name填寫域名
 
# cp aoshiwei.com.key aoshiwei.com.key.bak
# openssl rsa -in aoshiwei.com.key.bak -out aoshiwei.com.key
# openssl x509 -req -days 365 -in aoshiwei.com.csr -signkey aoshiwei.com.key -out aoshiwei.com.crt

在nginx.conf中新增:
  1. server {  
  2.         ### server port and name ###  
  3.         listen          443 ssl;  
  4.         server_name     member.aoshiwei.com;  
  5.         ssl on;  
  6.         ### SSL log files ###  
  7.         access_log      logs/ssl-access.log;  
  8.         error_log       logs/ssl-error.log;  
  9.         ### SSL cert files ###  
  10.         ssl_certificate      ssl/aoshiwei.com.crt;  
  11.         ssl_certificate_key  ssl/aoshiwei.com.key;  
  12.         ### Add SSL specific settings here ###  
  13.         keepalive_timeout    60;  
  14.         ###  Limiting Ciphers ########################  
  15.         # Uncomment as per your setup  
  16.         #ssl_ciphers HIGH:!ADH;  
  17.         #ssl_perfer_server_ciphers on;  
  18.         #ssl_protocols SSLv3;  
  19.         ##############################################  
  20.         ### We want full access to SSL via backend ###  
  21.         location / {  
  22.                 proxy_pass  http://member.aoshiwei.com;  
  23.                 ### force timeouts if one of backend is died ##  
  24.                 proxy_next_upstream error timeout invalid_header http_500 http_502 http_503;  
  25.                 ### Set headers ####  
  26.                 proxy_set_header Host $host;  
  27.                 proxy_set_header X-Real-IP $remote_addr;  
  28.                 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;  
  29.                 ### Most PHP, Python, Rails, Java App can use this header ###  
  30.                 proxy_set_header X-Forwarded-Proto https;  
  31.                 ### By default we don't want to redirect it ####  
  32.                 proxy_redirect     off;  
  33.                 }  
  34.       }