nginx使用ssl模塊配置支持HTTPS訪問


默認情況下ssl模塊並未被安裝,如果要使用該模塊則需要在編譯nginx時指定–with-http_ssl_module參數.

需求:
做一個網站域名為 www.localhost.cn 要求通過https://www.localhost.cn進行訪問.

10.10.100.8 www.localhost.cn

實驗步驟:

1.首先確保機器上安裝了openssl和openssl-devel

#yum install openssl
#yum install openssl-devel

2.創建服務器私鑰,命令會讓你輸入一個口令:

openssl genrsa -des3 -out server.key 1024  //生成私鑰 
#因為以后要給nginx使用.每次reload nginx配置時候都要你驗證這個PAM密碼的.由於生成時候必須輸入密碼,你可以輸入后 再刪掉。

3.創建簽名請求的證書(CSR):

openssl req -new -key server.key -out server.csr								//生成證書頒發機構,用於頒發公鑰 

4.在加載SSL支持的Nginx並使用上述私鑰時除去必須的口令:

cp server.key server.key.org
openssl rsa -in server.key.org -out server.key									//除去密碼以便reload詢問時不需要密碼

5.配置nginx
最后標記證書使用上述私鑰和CSR:

openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt

6.修改Nginx配置文件,讓其包含新標記的證書和私鑰:

#vim /usr/local/nginx/conf/nginx.conf 
http {

        include server/*.cn;
}

7.修改Nginx配置文件,讓其包含新標記的證書和私鑰:

#vim /usr/local/nginx/server/www.localhost.cn
server { 
        listen       443;  																		//監聽端口為443 
        server_name  www.localhost.cn; 
 
        ssl                  on;  		          //開啟ssl 
        ssl_certificate      /etc/pki/tls/certs/server.crt;  	 //證書位置 
        ssl_certificate_key  /etc/pki/tls/certs/server.key;  	 //私鑰位置 
        ssl_session_timeout  5m; 
        ssl_protocols  SSLv2 SSLv3 TLSv1;  		     //指定密碼為openssl支持的格式 
        ssl_ciphers  HIGH:!aNULL:!MD5; 				//密碼加密方式 
        ssl_prefer_server_ciphers   on; 			//依賴SSLv3和TLSv1協議的服務器密碼將優先於客戶端密碼 
 
        location / { 
            root   html;    					//根目錄的相對位置 
            index  index.html index.htm; 
        } 
    } 

8.啟動nginx服務器.

#/usr/local/nginx/sbin/nginx -s reload //如果環境允許的話直接殺掉進程在啟動nginx

如果出現“[emerg] 10464#0: unknown directive "ssl" in /usr/local/nginx-0.6.32/conf/nginx.conf:74”則說明沒有將ssl模塊編譯進nginx,在configure的時候加上“--with-http_ssl_module”即可

如:[root@localhost nginx-1.4.4]# ./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --with-http_ssl_module

9.測試網站是否能夠通過https訪問

https://www.localhost.cn

另外還可以加入如下代碼實現80端口重定向到443

server {
listen 80;
server_name www.localhost.cn;
#rewrite ^(.*) https://$server_name$1 permanent;
rewrite ^(.*)$  https://$host$1 permanent;
}

過以下配置,可以設置一個虛擬主機同時支持HTTP和HTTPS

listen 80;
listen 443 default ssl;

同時支持80和443同時訪問配置:

server {
    listen      80 default backlog=2048;
    listen      443 ssl;
    server_name  www.localhost.com;
   
    #ssl on;  //注釋掉
    ssl_certificate   /usr/local/https/www.localhost.com.crt;
    ssl_certificate_key  /usr/local/https/www.localhost.com.key;
    ssl_session_timeout 5m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers AESGCM:ALL:!DH:!EXPORT:!RC4:+HIGH:!MEDIUM:!LOW:!aNULL:!eNULL;
    ssl_prefer_server_ciphers on;

Nginx 設置忽略favicon.ico文件的404錯誤日志(關閉favicon.ico不存在時記錄日志)

在 server { … }內添加如下信息.

location = /favicon.ico {
log_not_found off;
access_log off;
}

 

參考文檔:http://dreamfire.blog.51cto.com/418026/1141302/

    http://www.nginx.cn/265.html

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM