nginx配置https訪問
一、https簡介
HTTPS其實是有兩部分組成:HTTP + SSL/TLS,也就是在HTTP上又加了一層處理加密信息的模塊。服務端和客戶端的信息傳輸都會通過TLS進行加密,所以傳輸的數據都是加密後的數據
二、https協議原理
首先,客戶端與服務器建立連接,各自生成私鑰和公鑰,是不同的。服務器返給客戶端一個公鑰,然後客戶端拿着這個公鑰把要搜索的東西加密,稱之爲密文,並連並自己的公鑰一起返回給服務器,服務器拿着自己的私鑰解密密文,然後把響應到的數據用客戶端的公鑰加密,返回給客戶端,客戶端拿着自己的私鑰解密密文,把數據呈現出來。
三、SSL證書和私鑰的生成
1、創建一個保存私鑰和證書的目錄並進入
[root@localhost ~]# mkdir /usr/local/nginx-1.12.1/key
[root@localhost ~]# cd /usr/local/nginx-1.12.1/key
2、生成私鑰文件
[root@localhost key]# openssl genrsa -out server.key 1024
3、生成csr文件
[root@localhost key]# openssl req -new -key server.key -out certreq.csr
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:cn 所在國家的ISO標準代號
State or Province Name (full name) []:beijing 單位所在地省/自治區/直轄市
Locality Name (eg, city) [Default City]:beijing 單位所在地的市/縣/區
Organization Name (eg, company) [Default Company Ltd]:lvdian 單位/機構/企業合法的名稱
Organizational Unit Name (eg, section) []:yunwei 部門名稱
Common Name (eg, your name or your server's hostname) []:www.long.com
主機名,此項必須與您訪問提供SSL服務的服務器時所應用的域名完全匹配
Email Address []:[email protected] 郵件地址,不必輸入,直接回車跳過
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []: 以下信息不必輸入,回車跳過直到命令執行完畢
An optional company name []:
生成類型爲X509的自簽名證書。有效期設置3650天,即有效期爲10年
[root@localhost key]# openssl x509 -req -days 3650 -in server.csr -signkey server.key -out server.crt
四、重新編譯nginx添加ssl模塊(編譯過ssl跳過此步驟)
1、查看nginx版本及編譯參數
[root@localhost ~]# /usr/local/nginx-1.12.1/sbin/nginx -V
nginx version: nginx/1.12.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-4) (GCC)
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --user=nginx --group=nginx --prefix=/usr/local/nginx-1.12.1 --with-http_stub_status_module --with-http_ssl_module
可以看到,我們上面已經編譯了ssl模塊,如果沒有編譯ssl模塊,我們需要安裝以下方法進行編譯安裝。
2、編譯ssl模塊
(1)備份原有的nginx執行文件
[root@localhost ~]# cp /usr/local/nginx-1.12.1/sbin/nginx /usr/local/nginx-1.12.1/sbin/nginx.bak
(2)進入源碼解壓的nginx目錄編譯ssl模塊
提示:一定要把之前編譯過的參數都加進去重新編譯,並加入ssl模塊
[root@localhost ~]# cd nginx-1.12.1
[root@localhost nginx-1.12.1]# ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx-1.12.1 --with-http_stub_status_module --with-http_ssl_module
(3)執行make,千萬別執行make install,否則就覆蓋安裝
[root@localhost nginx-1.12.1]# make
3、停止nginx,將新生成的nginx文件覆蓋原有的nginx文件
make完之後在nginx-1.12.1/objs目錄下就多了個nginx,這個就是新版本的nginx執行文件了,將這個文件複製到/usr/local/nginx-1.12.1/sbin/目錄覆蓋原來的nginx執行文件。
[root@localhost nginx-1.12.1]# cp objs/nginx /usr/local/nginx-1.12.1/sbin/
cp:是否覆蓋"/usr/local/nginx-1.12.1/sbin/nginx"? y
4、測試新的nginx程序是否正確並查看編譯參數
[root@localhost ~]# /usr/local/nginx-1.12.1/sbin/nginx -t
nginx: the configuration file /usr/local/nginx-1.12.1/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx-1.12.1/conf/nginx.conf test is successful
[root@localhost ~]# /usr/local/nginx-1.12.1/sbin/nginx -v
nginx version: nginx/1.12.1
[root@localhost ~]# /usr/local/nginx-1.12.1/sbin/nginx -V
nginx version: nginx/1.12.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-4) (GCC)
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --user=nginx --group=nginx --prefix=/usr/local/nginx-1.12.1 --with-http_stub_status_module --with-http_ssl_module
五、nginx配置ssl加密
這裏的配置是最重要的,可以根據自己的需求進行配置。想要https就要監聽443端口,nginx.conf已經預留出了server,只要我們放開權限,簡單修改即可。
[root@localhost ~]# vim /usr/local/nginx-1.12.1/conf/nginx.conf
server {
listen 443 ssl;
server_name localhost;
ssl_certificate /usr/local/nginx-1.12.1/key/certreq.csr;
ssl_certificate_key /usr/local/nginx-1.12.1/key/server.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
root html;
index index.html index.htm;
}
}
#配置端口轉發
server {
listen 80;
server_name www.long.com;
rewrite ^(.*) https://$server_name$1 permanent;
}
[root@localhost ~]# /usr/local/nginx-1.12.1/sbin/nginx -s reload
ssl_certificate證書其實是個公鑰,它會被髮送到連接服務器的每個客戶端,ssl_certificate_key私鑰是用來解密的,所以它的權限要得到保護但nginx的主進程能夠讀取。當然私鑰和證書可以放在一個證書文件中,這種方式也只有公鑰證書才發送到client。
ssl_session_timeout 客戶端可以重用會話緩存中ssl參數的過期時間,內網系統默認5分鐘太短了,可以設成30m即30分鐘甚至4h。
ssl_ciphers選擇加密套件,不同的瀏覽器所支持的套件(和順序)可能會不同。這裏指定的是OpenSSL庫能夠識別的寫法,你可以通過 openssl -v cipher ‘RC4:HIGH:!aNULL:!MD5’(後面是你所指定的套件加密算法) 來看所支持算法。
ssl_prefer_server_ciphers on設置協商加密算法時,優先使用我們服務端的加密套件,而不是客戶端瀏覽器的加密套件。
六、Windows瀏覽器測試能否跳轉https成功
1、Windows中添加域名解析
打開windows的C:\Windows\System32\drivers\etc\hosts文件,添加下面的域名解析
192.168.10.10 www.long.com
2、打開瀏覽器,輸入域名測試
可以看到,我們的https已經跳轉成功
上面的是自己生成的證書,不受各個瀏覽器信任的,要讓各個瀏覽器信任就得在相關官方網站申請一個免費證書,一般免費證書有效期是幾個月至一年。