一、Nginx負載均衡
負載均衡:單從字面上的意思來了解就可以解釋N台伺服器平均分擔負載,不會因為某台伺服器負載高當機而某台伺服器閑置的情況。那麼負載均衡的前提就是要有多台伺服器才能實作,也就是兩台以上即可。
在開始部署負載均衡之前,我們先來介紹一個指令,dig指令需要yum安裝一下
[root@lnmp ~]# yum install bind-utils
[root@lnmp ~]# dig qq.com (dig後加域名,他可以傳回2個ip.實則域名解析,我們就用這兩個ip測試負載均衡)
; <<>> DiG 9.9.4-RedHat-9.9.4-51.el7_4.1 <<>> qq.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 57513
;; flags: qr rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 0
;; QUESTION SECTION:
;qq.com. IN A
;; ANSWER SECTION:
qq.com. 601 IN A 125.39.240.113
qq.com. 601 IN A 61.135.157.156
[root@lnmp ~]# vim /usr/local/nginx/conf/vhost/load.conf (再來編寫一個配置檔案,需要用到upstream子產品,upstream:資料轉發功能,為nginx提供了跨越單機的橫向處理能力,使nginx擺脫隻能為終端節點提供單一功能的限制,而使它具備了網路應用級别的拆分、封裝和整合的戰略功能。)
upstream qq
{
ip_hash; (負載均衡有多個web伺服器,我們需要一個長連接配接來保持于一個伺服器的連結,這裡需要用到hash)
server 61.135.157.156:80;
server 125.39.240.113:80;
}
server
listen 80;
server_name www.qq.com;
location /
{
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
檢查文法錯誤并且重新加載配置檔案。
[root@lnmp ~]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@lnmp ~]# /usr/local/nginx/sbin/nginx -s reload
nginx不支援代理Https服務。也就是說不支援通路web伺服器的443端口。
二、SSL原理
https和http相比,https的通信是加密的。如果不加密,比如你通路一個很重要的網站,資料包還是會到達,但是可能會用人從中間複制一份。https會把資料包加密,就算從中間複制也無法解碼。
https的工作流程:

1.浏覽器發送一個https的請求給伺服器。
2.伺服器有一套數字證書,可以自己制作也可以向組織申請,差別積水自己頒發的證書需要用戶端驗證通過,才可以繼續通路,而使用受信任的公司申請的證書則不會彈出 (這套證書其實就是一對公鑰和私鑰)
3.伺服器會把公鑰傳輸給用戶端
4.用戶端(浏覽器)收到公鑰後,會驗證其是否合法有效,無效會有警告提醒,有效則會生成一串随機字元串,并用收到的公鑰加密。
5.用戶端把加密的随機字元串傳輸給伺服器
6.伺服器收到加密随機字元串後,先用私鑰解密,擷取到這一串随機數後,再用這串随機字元串加密傳輸的資料(該加密為對稱加密,也就是将資料和這個随機字元串通過某種算法混合一起,這一除非知道私鑰,否則無法獲7.取資料内容)
8.伺服器把加密後的資料傳輸給用戶端。
9.用戶端收到資料後,在用自己的私鑰也就是那個随機字元串解密。
三、Nginx配置ssl
Nginx配置ssl
[root@lnmp nginx-1.8.0]# vim /usr/local/nginx/conf/vhost/ssl.conf (編寫ssl的配置檔案)
listen 443; (監聽443端口)
server_name lty.com; (編寫server_name)
index index.html index.php;
root /data/wwwroot/lty.com;
ssl on; (開啟ssl服務)
ssl_certificate lty.crt; (指定公鑰)
ssl_certificate_key lty.key; (指定私鑰)
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; (指定三種模式)
[root@lnmp nginx-1.8.0]# /usr/local/nginx/sbin/nginx -t (如果nginx編譯的時候沒有加上ssl,這裡會報錯需要重新編譯)
重新編譯:
[root@lnmp nginx-1.8.0]# cd /usr/local/src/nginx-1.8.0
[root@lnmp nginx-1.8.0]# ./configure --help |grep -i ssl
--with-http_ssl_module enable ngx_http_ssl_module
--with-mail_ssl_module enable ngx_mail_ssl_module
--with-openssl=DIR set path to OpenSSL library sources
--with-openssl-opt=OPTIONS set additional build options for OpenSSL
[root@lnmp nginx-1.8.0]# ./configure --prefix=/usr/local/nginx/ --with-http_ssl_module
[root@lnmp nginx-1.8.0]# make && make install
編譯完成後就可以檢查文法錯誤了,然後重新啟動
[root@lnmp nginx-1.8.0]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx//conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx//conf/nginx.conf test is successful
[root@lnmp nginx-1.8.0]# /etc/init.d/nginx restart
Restarting nginx (via systemctl): [ 确定 ]
建立測試頁面:
[root@lnmp nginx-1.8.0]# mkdir /data/wwwroot/lty.com
[root@lnmp nginx-1.8.0]# vim /data/wwwroot/lty.com/index.html
因為是我們自己辦法的證書,直接修改/etc/hosts,用Curl測試并看不出效果,提示證書已經失去信任。
[root@lnmp nginx-1.8.0]# vim /etc/hosts
127.0.0.1 lty.com
[root@lnmp nginx-1.8.0]# curl https://lty.com
curl: (60) Peer's certificate issuer has been marked as not trusted by the user.
More details here: http://curl.haxx.se/docs/sslcerts.html
curl performs SSL certificate verification by default, using a "bundle"
of Certificate Authority (CA) public keys (CA certs). If the default
bundle file isn't adequate, you can specify an alternate file
using the --cacert option.
If this HTTPS server uses a certificate signed by a CA represented in
the bundle, the certificate verification probably failed due to a
problem with the certificate (it might be expired, or the name might
not match the domain name in the URL).
If you'd like to turn off curl's verification of the certificate, use
the -k (or --insecure) option.
編輯windows的hosts。
192.168.52.101 lty.com
用浏覽器打開
<a href="https://lty.com/">https://lty.com</a>
如果通路不到,檢視防火牆資訊簡單的辦法直接-F
12306網站是自己頒發的證書:(在中國的政府有些網站,認為隻有自己的頒發的安全,是以用自己頒發的證書)
如果想要買證書,可以搜尋 沃通,
本文轉自 小新銳 51CTO部落格,原文連結:http://blog.51cto.com/13407306/2059168,如需轉載請自行聯系原作者