LNMP-Nginx配置SSL


SLL工作流程:
瀏覽器發送一個https的請求給服務器;
 服務器要有一套數字證書,可以自己制作(后面的操作就是阿銘自己制作的證書),也可以向組織申請,區別就是自己頒發的證書需要客戶端驗證通過,才可以繼續訪問,而使用受信任的公司申請的證書則不會彈出>提示頁面,這套證書其實就是一對公鑰和私鑰;
 服務器會把公鑰傳輸給客戶端;
 客戶端(瀏覽器)收到公鑰后,會驗證其是否合法有效,無效會有警告提醒,有效則會生成一串隨機數,並用收到的公鑰加密;
 客戶端把加密后的隨機字符串傳輸給服務器;
 服務器收到加密隨機字符串后,先用私鑰解密(公鑰加密,私鑰解密),獲取到這一串隨機數后,再用這串隨機字符串加密傳輸的數據(該加密為對稱加密,所謂對稱加密,就是將數據和私鑰也就是這個隨機字符串>通過某種算法混合在一起,這樣除非知道私鑰,否則無法獲取數據內容);
 服務器把加密后的數據傳輸給客戶端;
 客戶端收到數據后,再用自己的私鑰也就是那個隨機字符串解密;

 

 

 
 
一、實驗
 
配置SSL之前,需要檢查nginx是否有該模塊--with-http_ssl_module,如果沒有該模塊需要重新編譯nginx,具體操作參考nginx編譯安裝文檔,openssl該命令需要安裝openssl包獲得!!
 
 
1:生成私鑰
[root@proxy conf ~]# openssl genrsa -des3 -out tmp.key 2048
 
2:轉換私鑰,取消密碼
[root@proxy conf ~]# openssl rsa -in tmp.key -out test.key
 
3:刪除原私鑰文件
[root@proxy conf ~]# rm -f tmp.key
 
4:生成證書請求文件,需要拿這個文件和私鑰一起生產公鑰文件
[root@proxy conf ~]# openssl req -new -key test.key -out test.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]:CA
State or Province Name (full name) []:CA
Locality Name (eg, city) [Default City]:CA
Organization Name (eg, company) [Default Company Ltd]:CA
Organizational Unit Name (eg, section) []:CA
Common Name (eg, your name or your server's hostname) []:test
Email Address []:CA
 
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
 
 
5: 自己簽發證書
[root@proxy conf ~]# openssl x509 -req -days 365 -in test.csr -signkey test.key -out test.crt
Signature ok
subject=/C=CA/ST=CA/L=CA/O=CA/OU=CA/CN=test/emailAddress=CA
Getting Private key
 
 
 
6:生成之后,配置nginx配置文件
[root@proxy vhosts ~]# vim test.conf
server
{
listen 443; ##開啟https監聽的443端口
server_name www.test.com;
index index.html index.php;
ssl on; ##on表示開啟SSL,off關閉。
ssl_certificate test.crt; ##填寫證書的名稱
ssl_certificate_key test.key; ##填寫秘鑰的名稱
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
location /{
proxy_pass http:///192.168.1.10:8088;
proxy_set_header Host $proxy_Host;
}
}
說明:如果以上配置訪問只能實現https訪問,如果實現http和https同時能夠進行訪問,需要去掉ssl on這一項配置, 在listen 443 后面加ssl即可,注意需要將兩個server分開寫,寫在一個server里會有問題,配置如下
server {
listen 80;
server_name www.test.com ;
access_log /data/nginx_log/test-access.log;
error_log /data/nginx_log/test-error.log;
rewrite ^(.*)$ https://www.test.com/$1 permanent; ##永久重定向,訪問網頁強制跳轉到https
location /{
proxy_pass http:///192.168.1.10:8088;
proxy_set_header Host $proxy_Host;
}
}
 
server{
listen 443 ssl;
server_name www.test.com;
ssl_certificate test.crt;
ssl_certificate_key test.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
location /{
proxy_pass http://192.168.1.10:8088;
proxy_set_header Host $proxy_Host;
}
}

 


免責聲明!

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



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