修改host文件,為最后訪問域名准備
C:\Windows\System32\drivers\etc host文件目錄
192.168.10.140 www.joyce.com 在最后添加這個自定義域名
https公鑰和私鑰定義
服務端:公鑰、私鑰
服務器持有一對公鑰和私鑰,並且把自己的公鑰發給客戶端。
當瀏覽器發起申請時,數據通過瀏覽器端的私鑰加密發送給服務端。服務端拿到加密密文時,通過瀏覽器的公鑰解密得到數據。
服務端再通過自己的私鑰加密返回數據到瀏覽器,瀏覽器拿到密文后通過服務端的公鑰解密得到數據。
下載nginx和安裝
cd /usr/local
wget http://nginx.org/download/nginx-1.14.0.tar.gz 下載
tar -zxvf nginx-1.14.0.tar.gz 解壓
/usr/local/nginx/sbin/nginx -V 查看ngixn版本極其編譯參數
cd nginx-1.14.0 進入nginx源碼目錄
./configure
#make & make install 編譯和安裝
要添加ssl加密模塊,需重新編譯模塊
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-file-aio --with-http_realip_module
make 千萬別make & make install,否則就覆蓋安裝了。make完之后在objs目錄下就多了個nginx,這個就是新版本的程序了
cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak 備份舊的nginx程序
rm -rf /usr/local/nginx/sbin/nginx 先刪除舊的nginx程序
cp objs/nginx /usr/local/nginx/sbin/nginx 把新的nginx程序覆蓋舊的
生成https crt證書文件
yum -y update 更新yum源
yum -y install openssl 安裝ssl
cd /usr/local/nginx
mkdir ssl 創建ssl文件夾
cd ssl
openssl genrsa -des3 -out server.key 1024 生成server.key私鑰文件,長度為1024,需要指定一個密碼:123456
-out filename :將生成的私鑰保存至filename文件,若未指定輸出文件,則為標准輸出。 -numbits :指定要生成的私鑰的長度,默認為1024。該項必須為命令行的最后一項參數。 -des|-des3|-idea:指定加密私鑰文件用的算法,這樣每次使用私鑰文件都將輸入密碼,太麻煩所以很少使用。 -passout args :加密私鑰文件時,傳遞密碼的格式,如果要加密私鑰文件時單未指定該項,則提示輸入密碼。傳遞密碼的args的格式見openssl密碼格式。
openssl req -new -key server.key -out server.csr 生成server.csr公鑰文件,需要輸入server.key里指定的密碼,我這里是:123456
[root@192 ssl]# openssl req -new -key server.key -out server.csr
Enter pass phrase for server.key:
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]:cs
State or Province Name (full name) []:www.joyce.com
Locality Name (eg, city) [Default City]:ShangHai
Organization Name (eg, company) [Default Company Ltd]:joyce
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) []:joyce
Email Address []:
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
[root@192 ssl]#
以上這些后面帶[]的都是選填,可以直接回車不填。
接下來去除私鑰的口令驗證,也就是去除用戶名密碼登錄校驗
cp server.key server.key.org 先復制一份
openssl rsa -in server.key.org -out server.key 去除口令后,覆蓋server.key文件。需要輸入server.key里指定的密碼:123456
openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt 標記證書使用私鑰和csr,使用x509格式,有效期限365天
[root@192 ssl]# openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt Signature ok subject=/C=cs/ST=www.joyce.com/L=ShangHai/O=joyce/CN=joyce Getting Private key
注意!server.crt 就是我們需要的證書!
nginx.conf 配置https配置
vim /usr/local/nginx/conf/nginx.conf
worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on;
server {
listen 80;
server_name www.joyce.com;
return 301 https://$server_name$request_uri; 301重定向到https協議端口,這樣訪問http://www.joyce.com會自動跳轉到https://www.joyce.com
# 可以參考:https://www.cnblogs.com/liuq1991/p/9019900.html (nginx http轉 https)
}
server { listen 443 ssl; server_name www.joyce.com; ssl on; 啟用https協議訪問 ssl_certificate /usr/local/nginx/ssl/server.crt; 服務端公鑰 ssl_certificate_key /usr/local/nginx/ssl/server.key; 服務端私鑰 error_log /usr/local/nginx/logs/error443.log; location / { proxy_pass http://192.168.10.140:8761; 訪問應用 } } }
測試新的nginx.conf是否配置正確
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf -t
輸出如下結果代表nginx.conf配置文件無誤:
nginx: theconfiguration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx:configuration file /usr/local/nginx/conf/nginx.conf test issuccessful
平滑重啟nginx
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf -s reload
查看ngixn版本極其編譯參數
/usr/local/nginx/sbin/nginx -V
開啟443端口(已關閉防火牆firewalld忽略這一步)
關閉防火牆 systemctl stop firewalld
開啟防火牆 systemctl start firewalld
查看防火牆狀態: systemctl status firewalld
如果防火牆被開啟,則有可能存在443端口沒有開啟監聽的情況
firewall-cmd --zone=public --add-port=443/tcp –permanent
firewall-cmd --reload
查看防火牆里添加的端口
firewall-cmd --list-ports
查看是否在防火牆里開啟了443端口監聽
netstat -antp|grep 443 結果:
tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN 2037/nginx:master //代表防火牆開啟,並監聽了在nginx程序,表示成功。
瀏覽器端輸入https://www.joyce.com 可以查看公鑰
注意!最后訪問的是https協議!而不是http!
在瀏覽器里輸入https://www.joyce.com/ 即可訪問成功!

