Nginx負載均衡
當(dāng)用戶訪問nginx定制好的域名時,nginx通過轉(zhuǎn)發(fā)到幾臺真實的站點,通過upstream實現(xiàn)
[root@centos7 vhost]# vim /usr/local/nginx/conf/vhost/load.confupstream www.tt.com#自定義域名{# ip_ash; #保證同一個用戶始終保持在同一臺機器上,即當(dāng)域名指向多個IP時,保證每個用戶始終解析到同一IP server 192.168.3.74:80; server 192.168.3.83:80; #指定web服務(wù)器的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 安全套接層)協(xié)議,及其繼任者TLS(Transport Layer Security傳輸層安全)協(xié)議,是為網(wǎng)絡(luò)通信提供安全及數(shù)據(jù)完整性的一種安全協(xié)議。
瀏覽器發(fā)送一個https的請求給服務(wù)器;
服務(wù)器要有一套數(shù)字證書,可以自己制作,也可以向組織申請,區(qū)別就是自己頒發(fā)的證書需要客戶端驗證通過,才可以繼續(xù)訪問,而使用受信任的公司申請的證書則不會彈出>提示頁面,這套證書其實就是一對公鑰和私鑰;
服務(wù)器會把公鑰傳輸給客戶端;
客戶端(瀏覽器)收到公鑰后,會驗證其是否合法有效,無效會有警告提醒,有效則會生成一串隨機數(shù),并用收到的公鑰加密;
客戶端把加密后的隨機字符串傳輸給服務(wù)器;
服務(wù)器收到加密隨機字符串后,先用私鑰解密(公鑰加密,私鑰解密),獲取到這一串隨機數(shù)后,再用這串隨機字符串加密傳輸?shù)臄?shù)據(jù)(該加密為對稱加密,所謂對稱加密,就是將數(shù)據(jù)和私鑰也就是這個隨機字符串>通過某種算法混合在一起,這樣除非知道私鑰,否則無法獲取數(shù)據(jù)內(nèi)容);
服務(wù)器把加密后的數(shù)據(jù)傳輸給客戶端;
客戶端收到數(shù)據(jù)后,再用自己的私鑰也就是那個隨機字符串解密;
頒發(fā)的 證書必須得瀏覽器廠商認可的。
生成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、轉(zhuǎn)換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、生成證書請求文件
需要拿這個文件和私鑰一起生產(chǎn)公鑰文件:
[root@centos7 conf]# openssl req -new -key testssl.key -out testssl.csrCountry Name (2 letter code) [XX]:CNState or Province Name (full name) []:GDLocality Name (eg, city) [Default City]:GZOrganization Name (eg, company) [Default Company Ltd]:FCOrganizational Unit Name (eg, section) []:FCCommon Name (eg, your name or your server's hostname) []:testsslEmail Address []:[email protected] enter the following 'extra' attributesto be sent with your certificate requestA challenge password []:123456An optional company name []:123456
6、
[root@centos7 conf]# ls testssl.*testssl.csr testssl.key
7、創(chuàng)建公鑰
[root@centos7 conf]# openssl x509 -req -days 365 -in testssl.csr -signkey testssl.key -out testssl.crtSignature oksubject=/C=CN/ST=GD/L=GZ/O=FC/OU=FC/CN=testssl/[email protected] Private keyYou 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; #配置協(xié)議}
9、
[root@centos7 vhost]# /etc/init.d/nginx restart
10、
[root@centos7 vhost]# netstat -nutlp| grep 443tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN 7703/nginx: master
驗證:
由于自己申請的sll沒有得到瀏覽器的認可,所以被標(biāo)識為不安全。可以訪問
另外,關(guān)于阿里云的負載均衡配置可參考其官方說明文檔:https://help.aliyun.com/document_detail/27552.html
新聞熱點
疑難解答