1. 程式人生 > >LNMP(Nginx負載均衡,SSL原理,Nginx配置SSL,生產SSL密鑰對)

LNMP(Nginx負載均衡,SSL原理,Nginx配置SSL,生產SSL密鑰對)

orm mage 解碼 web服務 bind 先來 mkdir padding ddr

一、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_pass http://qq; (這裏寫的要與upstream一致,因為域名是虛擬的,下面的2個ip才是重要的)

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


[root@lnmp ~]# curl -x127.0.0.1:80 www.qq.com (發現返回的是qq頁面的源代碼)


nginx不支持代理Https服務。也就是說不支持訪問web服務器的443端口。


二、SSL原理

https和http相比,https的通信是加密的。如果不加密,比如你訪問一個很重要的網站,數據包還是會到達,但是可能會用人從中間復制一份。https會把數據包加密,就算從中間復制也無法解碼。


https的工作流程:

技術分享圖片

  1. 1.瀏覽器發送一個https的請求給服務器。

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

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

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

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

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

  7. 8.服務器把加密後的數據傳輸給客戶端。

  8. 9.客戶端收到數據後,在用自己的私鑰也就是那個隨機字符串解密。




三、Nginx配置ssl

Nginx配置ssl

[root@lnmp nginx-1.8.0]# vim /usr/local/nginx/conf/vhost/ssl.conf (編寫ssl的配置文件)

server

{

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

用瀏覽器打開

https://lty.com

如果訪問不到,查看防火墻信息簡單的辦法直接-F

技術分享圖片


12306網站是自己頒發的證書:(在中國的政府有些網站,認為只有自己的頒發的安全,所以用自己頒發的證書)

如果想要買證書,可以搜索 沃通,



LNMP(Nginx負載均衡,SSL原理,Nginx配置SSL,生產SSL密鑰對)