1. 程式人生 > >Nginx負載均衡、ssl原理、生成ssl密鑰對、Nginx配置ssl

Nginx負載均衡、ssl原理、生成ssl密鑰對、Nginx配置ssl

lnmp架構

Nginx負載均衡

當用戶訪問nginx定制好的域名時,nginx通過轉發到幾臺真實的站點,通過upstream實現

[root@centos7 vhost]# vim /usr/local/nginx/conf/vhost/load.conf


upstream www.tt.com

#自定義域名

{

# ip_ash;

#保證同一個用戶始終保持在同一臺機器上,即當域名指向多個IP時,保證每個用戶始終解析到同一IP

server 192.168.3.74:80;

server 192.168.3.83:80;

#指定web服務器的IP

}

server

{

listen 80;

server_name www.tt.com;

location /

{

proxy_pass http://tt.com;

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

}

}


ssl原理

SSL(Secure Sockets Layer 安全套接層)協議,及其繼任者TLS(Transport Layer Security傳輸層安全)協議,是為網絡通信提供安全及數據完整性的一種安全協議。

技術分享

瀏覽器發送一個https的請求給服務器;

服務器要有一套數字證書,可以自己制作,也可以向組織申請,區別就是自己頒發的證書需要客戶端驗證通過,才可以繼續訪問,而使用受信任的公司申請的證書則不會彈出>提示頁面,這套證書其實就是一對公鑰和私鑰;

服務器會把公鑰傳輸給客戶端;

客戶端(瀏覽器)收到公鑰後,會驗證其是否合法有效,無效會有警告提醒,有效則會生成一串隨機數,並用收到的公鑰加密;

客戶端把加密後的隨機字符串傳輸給服務器;

服務器收到加密隨機字符串後,先用私鑰解密(公鑰加密,私鑰解密),獲取到這一串隨機數後,再用這串隨機字符串加密傳輸的數據(該加密為對稱加密,所謂對稱加密,就是將數據和私鑰也就是這個隨機字符串>通過某種算法混合在一起,這樣除非知道私鑰,否則無法獲取數據內容);

服務器把加密後的數據傳輸給客戶端;

客戶端收到數據後,再用自己的私鑰也就是那個隨機字符串解密;


頒發的 證書必須得瀏覽器廠商認可的。


生成ssl密鑰對

首先對讓nginx支持ssl模塊

1、[root@centos7 nginx-1.12.1]# cd /data/package/nginx-1.12.1

2、[root@centos7 nginx-1.12.1]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module

3、make

4、make install

正式操作:

1、[root@centos7 vhost]# cd /usr/local/nginx/conf/

2、輸入密碼

[root@centos7 conf]# openssl genrsa -des3 -out tmp.key 2048

3、轉換key,取消密碼:

[root@centos7 conf]# openssl rsa -in tmp.key -out testssl.key

Enter pass phrase for tmp.key: 輸入第2步的密碼

4、刪除密鑰文件:

[root@centos7 conf]# rm -f tmp.key

5、生成證書請求文件

需要拿這個文件和私鑰一起生產公鑰文件:

[root@centos7 conf]# openssl req -new -key testssl.key -out testssl.csr


Country Name (2 letter code) [XX]:CN

State or Province Name (full name) []:GD

Locality Name (eg, city) [Default City]:GZ

Organization Name (eg, company) [Default Company Ltd]:FC

Organizational Unit Name (eg, section) []:FC

Common Name (eg, your name or your server‘s hostname) []:testssl

Email Address []:[email protected]


Please enter the following ‘extra‘ attributes

to be sent with your certificate request

A challenge password []:123456

An optional company name []:123456

6、

[root@centos7 conf]# ls testssl.*

testssl.csr testssl.key

7、創建公鑰

[root@centos7 conf]# openssl x509 -req -days 365 -in testssl.csr -signkey testssl.key -out testssl.crt

Signature ok

subject=/C=CN/ST=GD/L=GZ/O=FC/OU=FC/CN=testssl/[email protected]

Getting Private key

You have new mail in /var/spool/mail/root

[root@centos7 conf]# ls testssl.*

testssl.crt testssl.csr testssl.key


8、nginx配置ssl

[root@centos7 vhost]# vi ssl.conf


server

{

listen 443;

server_name testssl.com;

index index.html index.php;

root /data/wwwroot/ssl.com;

ssl on;

#開啟ssl

ssl_certificate testssl.crt;

#配置公鑰

ssl_certificate_key testssl.key;

#配置私鑰

ssl_protocols TLSv1 TLSv1.1 TLSv1.2;

#配置協議

}

9、[root@centos7 vhost]# /etc/init.d/nginx restart

10、[root@centos7 vhost]# netstat -nutlp| grep 443

tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN 7703/nginx: master


驗證:

由於自己申請的sll沒有得到瀏覽器的認可,所以被標識為不安全。可以訪問

技術分享



本文出自 “探索發現新事物” 博客,請務必保留此出處http://jacksoner.blog.51cto.com/5802843/1982442

Nginx負載均衡、ssl原理、生成ssl密鑰對、Nginx配置ssl