93.Nginx配置:負載均衡和SSL配置

一、負載均衡

負載均衡在服務端開發中算是一個比較重要的特性。因爲Nginx除了作爲常規的Web服務器外,還會被大規模的用於反向代理前端,因爲Nginx的異步框架可以處理很大的併發請求,把這些併發請求hold住之後就可以分發給後臺服務端(backend servers,也叫做服務池, 後面簡稱backend)來做複雜的計算、處理和響應,這種模式的好處是相當多的:隱藏業務主機更安全,節約了公網IP地址,並且在業務量增加的時候可以方便地擴容後臺服務器。
負載均衡可以分爲硬件負載均衡和軟件負載均衡,前者一般是專用的軟件和硬件相結合的設備,設備商會提供完整成熟的解決方案,通常也會更加昂貴。軟件的複雜均衡以Nginx佔據絕大多數,本文也是基於其手冊做相應的學習研究的。

1、修改虛擬主機配置文件(以qq.com爲例)

[root@sdwaqw ~]# cd /usr/local/nginx/conf/vhost/ [root@sdwaqw vhost]# dig qq.com //dig命令獲取IP,沒有dig命令,使用‘yum install -y bind-untils’安裝 ; <<>> DiG 9.9.4-RedHat-9.9.4-51.el7_4.2 <<>> qq.com ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 38970 ;; flags: qr rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 1 ;; OPT PSEUDOSECTION: ; EDNS: version: 0, flags:; udp: 4096 ;; QUESTION SECTION: ;qq.com. IN A ;; ANSWER SECTION: qq.com. 414 IN A 125.39.240.113 qq.com. 414 IN A 61.135.157.156 ;; Query time: 37 msec ;; SERVER: 119.29.29.29#53(119.29.29.29) ;; WHEN: 五 3月 16 22:00:18 CST 2018 ;; MSG SIZE rcvd: 67 //可以看到兩個IP,有兩個IP就可以走負載均衡了 [root@sdwaqw vhost]# vim load.conf //編輯配置文件,增加以下內容 #配置內容 upstream qq #名字自定義 { ip_hash; # 目的:同一個用戶保持在同一個服務器上 # 即當域名指向多個IP時,保證每個用戶始終解析到同一IP server 61.135.157.156:80; server 125.39.240.113:80; # 指定web服務器的IP } server { listen 80; server_name www.qq.com; location / { proxy_pass http://qq; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }

2、測試

[root@sdwaqw 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@sdwaqw vhost]# /usr/local/nginx/sbin/nginx -s reload [root@sdwaqw vhost]# curl -x127.0.0.1:80 www.qq.com -I HTTP/1.1 200 OK Server: nginx/1.12.2 Date: Fri, 16 Mar 2018 14:18:04 GMT Content-Type: text/html; charset=GB2312 Connection: keep-alive Vary: Accept-Encoding Vary: Accept-Encoding Expires: Fri, 16 Mar 2018 14:19:04 GMT Cache-Control: max-age=60 Vary: Accept-Encoding Vary: Accept-Encoding X-Cache: HIT from tianjin.qq.com //這裏如果不加-I選項也是200狀態碼,因爲有默認虛擬主機,不過其他提示不一樣
測試下不加-I選項
[root@sdwaqw vhost]# curl -x127.0.0.1:80 www.qq.com
結果如下圖:

93.Nginx配置:負載均衡和SSL配置

注意: Nginx不支持代理https,只能代理http。

二、Nginx配置SSL

SSL(Secure Sockets Layer 安全套接層)協議,及其繼任者TLS(Transport Layer Security傳輸層安全)協議,是爲網絡通信提供安全及數據完整性的一種安全協議。
1、瀏覽器發送一個https的請求給服務器;
2、服務器要有一套數字證書,可以自己製作(後面的操作就是用自己製作的證書),也可以向組織申請,區別就是自己頒發的證書需要客戶端驗證通過,纔可以繼續訪問,而使用受信任的公司申請的證書則不會彈出提示頁面,這套證書其實就是一對公鑰和私鑰;
3、服務器會把公鑰傳輸給客戶端;
4、客戶端(瀏覽器)收到公鑰後,會驗證其是否合法有效,無效會有警告提醒,有效則會生成一串隨機數,並用收到的公鑰加密;
5、客戶端把加密後的隨機字符串傳輸給服務器;
6、服務器收到加密隨機字符串後,先用私鑰解密(公鑰加密,私鑰解密),獲取到這一串隨機數後,再用這串隨機字符串加密傳輸的數據(該加密爲對稱加密,所謂對稱加密,就是將數據和私鑰也就是這個隨機字符串>通過某種算法混合在一起,這樣除非知道私鑰,否則無法獲取數據內容);
7、服務器把加密後的數據傳輸給客戶端;
8、客戶端收到數據(服務端公鑰加密)後,再用自己的私鑰也就是那個隨機字符串解密;

93.Nginx配置:負載均衡和SSL配置


1、生成自定義的SSL證書(僅坐試驗用)
[root@sdwaqw conf]# openssl genrsa -des3 -out tmp.key 2048 //沒有openssl命令,則通過“yum install -y openssl”安裝 Generating RSA private key, 2048 bit long modulus ...................................................................................+++ .......................................................................................................................................................+++ e is 65537 (0x10001) Enter pass phrase for tmp.key: Verifying - Enter pass phrase for tmp.key: //這一步操作是生成key即“私鑰”,2048爲加密字符長度,會讓我們輸入密碼,不能太短,否者不成功。 [root@sdwaqw conf]# openssl rsa -in tmp.key -out sdwaqw.key Enter pass phrase for tmp.key: writing RSA key //把tmp.key轉化成sdwaqw.key,目的是刪除剛纔設置的密碼,如果不清除密碼,後面很不方便 [root@sdwaqw conf]# rm -f tmp.key [root@sdwaqw conf]# openssl req -new -key sdwaqw.key -out sdwaqw.csr You are about to be asked to enter information that will be incorporated into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank For some fields there will be a default value, If you enter '.', the field will be left blank. ----- Country Name (2 letter code) [XX]:CN State or Province Name (full name) []:JS Locality Name (eg, city) [Default City]:SZ Organization Name (eg, company) [Default Company Ltd]:XXLtd Organizational Unit Name (eg, section) []:sdwaqw.com Common Name (eg, your name or your server's hostname) []:ZZ Email Address []:[email protected] Please enter the following 'extra' attributes to be sent with your certificate request A challenge password []:zzz123456 An optional company name []:z //生成證書請求文件,key文件和csr文件生成最終的公鑰文件。Common Name爲後面配置Nginx配置文件server_name [root@sdwaqw conf]# openssl x509 -req -days 365 -in sdwaqw.csr -signkey sdwaqw.key -out sdwaqw.crt Signature ok subject=/C=CN/ST=JS/L=C/O=C/OU=C/CN=sdwaqw.com/emailAddress=z Getting Private key [root@sdwaqw conf]# ls |grep sdwaqw sdwaqw.crt sdwaqw.csr sdwaqw.key //最終生成crt證書,也就是公鑰

2、配置Nginx支持SSL

1)、編輯配置文件

[root@sdwaqw vhost]# vim ssl.conf //寫入以下內容 server { listen 443; server_name sdwaqw.com; index index.html index.php; root /data/wwwroot/ssltest; ssl on; ssl_certificate sdwaqw.crt; ssl_certificate_key sdwaqw.key; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; }

2)、檢查配置是否有問題

[root@sdwaqw 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並不支持SSL,因爲之前Nginx編譯時並沒有配置支持SSL的參數,所以需要重新編譯一次,加上SSL參數:
[root@sdwaqw vhost]# cd /usr/local/src/nginx-1.12.2 [root@sdwaqw 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@sdwaqw nginx-1.12.2]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module [root@sdwaqw nginx-1.12.2]# make [root@sdwaqw nginx-1.12.2]#make install [root@sdwaqw 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@sdwaqw nginx-1.12.2]# /etc/init.d/nginx restart

3)、測試

在window的hosts文件中添加:192.168.242.128 sdwaqw.com
[root@sdwaqw vhost]# mkdir /data/wwwroot/ssltest [root@sdwaqw vhost]# echo "ssl test" > /data/wwwroot/ssltest/index.html

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