Nginx負載均衡,ssl原理

Nginx負載均衡

Nginx負載均衡是通過代理服務器讓後面的web服務器能更快更穩定,還可以避免單點設備的故障造成的服務不可用。

dig命令:常用的域名解析工具

通過安裝bind-utils這個包。

[root@shuai-01 ~]# yum install -y bind-utils

語法: dig 域名

[root@shuai-01 ~]# dig www.qq.com

做負載均衡,先寫一個新的配置文件(ld.conf)

[root@shuai-01 vhost]# vim ld.conf

upstream qq_com
##自定義名字
{
    ip_hash;
    ##保證同一個用戶始終在同一臺機器上,當域名指向多個IP時,保證用戶始終解析到同一IP
    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_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;
    }
}

檢測:

使用代理前:在本機訪問www.qq.com會直接訪問到默認虛擬主機的默認頁

[root@shuai-01 vhost]# curl -x127.0.0.1:80 www.qq.com
this is a default site

代理後:

[root@shuai-01 vhost]# /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@shuai-01 vhost]# /usr/local/nginx/sbin/nginx -s reload

會變成qq.com的主頁

Nginx不支持代理https 只能代理http,新版本Nginx能代理tcp

HTTP超文本傳輸協議(HyperText Transfer Protocol)是互聯網上應用最爲廣泛的一種網絡協議。 
HTTPS(全稱:Hyper Text Transfer Protocol over Secure Socket Layer),是以安全爲目標的HTTP通道,簡單講是HTTP的安全版。HTTPS協議是由SSL+HTTP協議構建的可進行加密傳輸、身份認證的網絡協議要比http協議安全。 
HTTP默認的端口號爲80,HTTPS的端口號爲443。 
TCP(Transmission Control Protocol 傳輸控制協議)是一種面向連接的、可靠的、基於字節流的傳輸層通信協議,由IETF的RFC 793定義。默認監聽80端口。

ssl原理

HTTPS它是一種加密的HTTPS協議,如果HTTPS通信的數據包在傳輸過程中被截獲,我們可以破譯這些數據包裏面的信息,這裏面不乏一些用戶名、密碼、手機號等敏感的信息,而如果使用HTTPS通信,即使數據包被截獲,我們也無法破譯裏面的內容。

這裏寫圖片描述

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

生成SSL密鑰對

理解了ssl原理後,現在我們可以在虛擬機上去生成ssl密鑰對,也就是自己製作證書。我們需要使用一個工具來生成密鑰對,把密鑰對放在nginx的conf目錄下。

進入nginx的conf目錄:

[root@shuai-01 ~]# cd /usr/local/nginx/conf/

我們需要使用到的工具是openssl,如果你虛擬機沒有此命令,需要自己安裝,安裝命令:

yum -y install openssl

準備完成後,第一步是生成一個私鑰,命令如下:

[root@shuai-01 conf]# openssl genrsa -des3 -out tmp.key 2048   //key文件爲私鑰

第二步,是把密碼取消掉,如果不取消的話,會每次都要求客戶端輸入此密碼,命令如下:

[root@shuai-01 conf]# openssl rsa -in tmp.key -out shuailinux.key

//轉換key,取消密碼

實際上這時候shuailinux.key和tmp.key是同一個文件,只不過前者有密碼,後者沒密碼。

這時候就可以把tmp.key給刪掉了:

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

第三步就是去生成一個請求的文件,生成這個請求文件的目的是爲了讓這個請求文件和私鑰一起去生成一個公鑰,命令如下,會要求你輸入一些信息,因爲是自己製作的證書所以隨便輸入也是可以的,如果是正式的證書就不可以隨便寫了:

[root@shuai-01 conf]# openssl req -new -key shuailinux.key -out shuailinux.csr

Country Name (2 letter code) [XX]:11
State or Province Name (full name) []:beijing
Locality Name (eg, city) [Default City]:beijing
Organization Name (eg, company) [Default Company Ltd]:shuai
Organizational Unit Name (eg, section) []:shuai
Common Name (eg, your name or your server's hostname) []:shuai
Email Address []:[email protected]

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:shuai
An optional company name []:shuai

第四步就是生成公鑰了,命令如下:

[root@shuai-01 conf]# openssl x509 -req -days 365 -in shuailinux.csr -si

gnkey shuailinux.key -out shuailinux.crt

[root@shuai-01 conf]# ls
fastcgi.conf            mime.types           shuailinux.crt
fastcgi.conf.default    mime.types.default   shuailinux.csr
fastcgi_params          nginx.conf           shuailinux.key
fastcgi_params.default  nginx.conf.1         uwsgi_params
htpasswd                nginx.conf.default   uwsgi_params.default
koi-utf                 scgi_params          vhost
koi-win                 scgi_params.default  win-utf

Nginx配置ssl

我們生成好密鑰對也就是證書之後,就可以使用Nginx配置SSL了。

在虛擬配置文件目錄下 
創建一個文件:

[root@shuai-01 conf]# cd vhost/
[root@shuai-01 vhost]# vim ssl.conf

加入以下內容:

server
{
   listen 443;
   server_name shuai.com;
   index index.html index.php;
   root /data/wwwroot/shuai.com;
   ssl on;  //定義開啓ssl
   ssl_certificate shuailinux.crt;  //指定公鑰
   ssl_certificate_key shuailinux.key;  //指定私鑰
   ssl_protocols TLSv1 TLSv1.1 TLSv1.2;  //定義協議
}

然後保存退出,測試一下配置文件

[root@shuai-01 vhost]# /usr/local/nginx/sbin/nginx -t
nginx: [emerg] unknown directive "ssl" in /usr/local/nginx/conf/vhost/ssl.conf:7
nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed

這裏寫圖片描述

查看Nginx的編譯參數:

[root@shuai-01 nginx-1.12.2]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.12.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-http_ssl_module

最早編譯nginx的 並沒有指定支持ssl ,需要重新編譯下,讓大家不要去刪除源碼包,後期有可能還要進一步編譯

查看將ssl模塊編譯進Nginx:

[root@shuai-01 vhost]# cd /usr/local/src/nginx-1.12.2/
[root@shuai-01 nginx-1.12.2]# ./configure --help |grep -i ssl
  --with-http_ssl_module             enable ngx_http_ssl_module
  --with-mail_ssl_module             enable ngx_mail_ssl_module
  --with-stream_ssl_module           enable ngx_stream_ssl_module
  --with-stream_ssl_preread_module   enable ngx_stream_ssl_preread_module
  --with-openssl=DIR                 set path to OpenSSL library sources
  --with-openssl-opt=OPTIONS         set additional build options for OpenSSL

重新編譯:

[root@shuai-01 nginx-1.12.2]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module

[root@shuai-01 nginx-1.12.2]# make

[root@shuai-01 nginx-1.12.2]# make install

檢查配置文件語法:

[root@shuai-01 nginx-1.12.2]# /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@shuai-01 nginx-1.12.2]# /etc/init.d/nginx restart

查監聽端口:

[root@shuai-01 nginx-1.12.2]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      5665/nginx: master  
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1968/sshd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      2806/master         
tcp        0      0 0.0.0.0:443             0.0.0.0:*               LISTEN      5665/nginx: master  
tcp6       0      0 :::3306                 :::*                    LISTEN      2754/mysqld         
tcp6       0      0 :::22                   :::*                    LISTEN      1968/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      2806/master  

這裏寫圖片描述

在/data/wwwroot/下創鍵shuai.com目錄:

[root@shuai-01 nginx-1.12.2]# cd /data/wwwroot/
[root@shuai-01 wwwroot]# mkdir shuai.com
[root@shuai-01 wwwroot]# cd shuai.com/
[root@shuai-01 shuai.com]# vim index.html

測試:

用curl :

[root@shuai-01 shuai.com]# curl -x127.0.0.1:443 https://shuai.com
curl: (56) Received HTTP code 400 from proxy after CONNECT

要想用curl訪問,寫host

[root@shuai-01 shuai.com]# vi /etc/hosts

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.172.3 www.baidu.com
127.0.0.1 shuai.com

[root@shuai-01 shuai.com]# curl  https://shuai.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添加一下

這裏寫圖片描述

打開瀏覽器:

這裏寫圖片描述

看防火牆規則:

全部清空或者添加一個443端口開放

[root@shuai-01 shuai.com]# iptables -F

這裏寫圖片描述

證書不被信任

信任證書要買,沃通。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章