【總結】Nginx實現HTTPS設定
HTTPS簡介
1.簡介
HTTPS其實是有兩部分組成:HTTP + SSL / TLS,也就是在HTTP上又加了一層處理加密資訊的模組。服務端和客戶端的資訊傳輸都會通過TLS進行加密,所以傳輸的資料都是加密後的資料。
2.協議原理
首先,客戶端與伺服器建立連線,各自生成私鑰和公鑰是不同的。伺服器返給客戶端一個公鑰,然後客戶端拿著這個公鑰把要搜尋的東西加密,稱之為密文,並連並自己的公鑰一起返回給伺服器,伺服器拿著自己的私鑰解密密文,然後把響應到的資料用客戶端的公鑰加密,返回給客戶端,客戶端拿著自己的私鑰解密密文,把資料呈現出來。
一、證書和私鑰的生成
注意:一般生成的目錄,應該放在nginx/conf/ssl目錄,而這裡是直接放在了nginx根目錄
1.建立伺服器證書金鑰檔案 server.key:
openssl genrsa -des3 -out server.key 1024
輸入密碼,確認密碼,自己隨便定義。記住它,後面要用到
2.建立伺服器證書的申請檔案 server.csr
openssl req -new -key server.key -out server.csr
輸出內容為:
Enter pass phrase for root.key: ← 輸入前面建立的密碼
Country Name (2 letter code) [AU]:CN ← 國家代號,中國輸入CN
State or Province Name (full name) [Some-State]:BeiJing ← 省的全名,拼音
Locality Name (eg, city) []:BeiJing ← 市的全名,拼音
Organization Name (eg, company) [Internet Widgits Pty Ltd]:MyCompany Corp. ← 公司英文名
Organizational Unit Name (eg, section) []: ← 可以不輸入
Common Name (eg, YOUR name) []: ← 此時不輸入
Email Address []:
Please enter the following ‘extra’ attributes
to be sent with your certificate request
A challenge password []: ← 可以不輸入
An optional company name []: ← 可以不輸入
3.備份一份伺服器金鑰檔案
cp server.key server.key.org
4.輸入檔案口令
openssl rsa -in server.key.org -out server.key
5.生成證書檔案server.crt
openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
整個流程,當出現Signature ok,則表示成功了
二、配置檔案
配置檔案 nginx-server/conf/nginx.conf內容
server {
listen 443 ssl;
server_name localhost;
ssl on;
ssl_certificate /opt/nginx-server/server.crt;
ssl_certificate_key /opt/nginx-server/server.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_prefer_server_ciphers on;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://tomcats;
}
}
三、開啟nginx的ssl模組
1.the "ssl" parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf:37
原因是nginx缺少http_ssl_module模組,編譯安裝時帶上--with-http_ssl_module配置就可以了
本部落格中Nginx相關文章:
------------------------------------------------------
------------------------------------------------------
期望和大家一起學習,共同進步,共勉
歡迎交流問題,可加個人QQ 469580884
或者,加我的群號 751925591,一起探討交流問題
不講虛的,只做實幹家
Talk is cheap,show me the code