nginx負載均衡實例分析

nginx之負載均衡

注,大家可以看到,由于我們網(wǎng)站是發(fā)展初期,nginx只代理了后端一臺服務器,但由于我們網(wǎng)站名氣大漲訪問的人越來越多一臺服務器實在是頂不住,于是我們加了多臺服務器,那么多臺服務器又怎么配置代理呢,我們這里以兩臺服務器為案例,為大家做演示。

1.upstream 負載均衡模塊說明

案例:

下面設定負載均衡的服務器列表。

upstream?test.net{ ip_hash; server?192.168.10.13:80; server?192.168.10.14:80?down; server?192.168.10.15:8009?max_fails=3?fail_timeout=20s; server?192.168.10.16:8080; } server?{ ?location?/?{ ??proxy_pass?http://test.net; ?} }

upstream是nginx的http upstream模塊,這個模塊通過一個簡單的調度算法來實現(xiàn)客戶端ip到后端服務器的負載均衡。在上面的設定中,通過upstream指令指定了一個負載均衡器的名稱test.net。這個名稱可以任意指定,在后面需要用到的地方直接調用即可。

2.upstream 支持的負載均衡算法

nginx的負載均衡模塊目前支持4種調度算法,下面進行分別介紹,其中后兩項屬于第三方調度算法。??

  • 輪詢(默認)。每個請求按時間順序逐一分配到不同的后端服務器,如果后端某臺服務器宕機,故障系統(tǒng)被自動剔除,使用戶訪問不受影響。weight 指定輪詢權值,weight值越大,分配到的訪問機率越高,主要用于后端每個服務器性能不均的情況下。

  • ip_hash。每個請求按訪問ip的hash結果分配,這樣來自同一個ip的訪客固定訪問一個后端服務器,有效解決了動態(tài)網(wǎng)頁存在的session共享問題。

  • fair。這是比上面兩個更加智能的負載均衡算法。此種算法可以依據(jù)頁面大小和加載時間長短智能地進行負載均衡,也就是根據(jù)后端服務器的響應時間來分配請求,響應時間短的優(yōu)先分配。nginx本身是不支持fair的,如果需要使用這種調度算法,必須下載nginx的upstream_fair模塊。

  • url_hash。此方法按訪問url的hash結果來分配請求,使每個url定向到同一個后端服務器,可以進一步提高后端緩存服務器的效率。nginx本身是不支持url_hash的,如果需要使用這種調度算法,必須安裝nginx 的hash軟件包。

3.upstream 支持的狀態(tài)參數(shù)

在http upstream模塊中,可以通過server指令指定后端服務器的ip地址和端口,同時還可以設定每個后端服務器在負載均衡調度中的狀態(tài)。常用的狀態(tài)有:?????

  • down,表示當前的server暫時不參與負載均衡。

  • backup,預留的備份機器。只有在其他所有非備用機器發(fā)生故障或忙碌時,才會向備用機器發(fā)送請求,因此備用機器的負載最輕。

  • max_fails,允許請求失敗的次數(shù),默認為1。如果次數(shù)超過最大次數(shù),則會返回由 proxy_next_upstream 模塊定義的錯誤。

  • 經(jīng)過多次失敗(達到max_fails次),服務將被暫停一段時間,并觸發(fā)fail_timeout。max_fails可以和fail_timeout一起使用。

注,當負載調度算法為ip_hash時,后端服務器在負載均衡調度中的狀態(tài)不能是weight和backup。

4.實驗拓撲

nginx負載均衡實例分析

5.配置nginx負載均衡

[root@nginx?~]#?vim?/etc/nginx/nginx.conf upstream?webservers?{ ???server?192.168.18.201?weight=1; ???server?192.168.18.202?weight=1; ?} ?server?{ ???listen????80; ???server_name?localhost; ???#charset?koi8-r; ???#access_log?logs/host.access.log?main; ???location?/?{ ???????proxy_pass???http://webservers; ???????proxy_set_header?x-real-ip?$remote_addr; ???} }

注,upstream是定義在server{ }之外的,不能定義在server{ }內部。定義好upstream之后,用proxy_pass引用一下即可。

6.重新加載一下配置文件

[root@nginx?~]#?service?nginx?reload nginx:?the?configuration?file?/etc/nginx/nginx.conf?syntax?is?ok nginx:?configuration?file?/etc/nginx/nginx.conf?test?is?successful 重新載入?nginx:??????????????????????[確定]

7.測試一下

nginx負載均衡實例分析

nginx負載均衡實例分析

注,大家可以不斷的刷新瀏覽的內容,可以發(fā)現(xiàn)web1與web2是交替出現(xiàn)的,達到了負載均衡的效果。

8.查看一下web訪問服務器日志

web1:

[root@web1?~]#?tail?/var/log/httpd/access_log 192.168.18.138?-?-?[04/sep/2013:09:41:58?+0800]?"get?/?http/1.0"?200?23?"-"?"mozilla/5.0?(compatible;?msie?10.0;?windows?nt?6.1;?wow64;?trident/6.0)" 192.168.18.138?-?-?[04/sep/2013:09:41:58?+0800]?"get?/?http/1.0"?200?23?"-"?"mozilla/5.0?(compatible;?msie?10.0;?windows?nt?6.1;?wow64;?trident/6.0)" 192.168.18.138?-?-?[04/sep/2013:09:41:59?+0800]?"get?/?http/1.0"?200?23?"-"?"mozilla/5.0?(compatible;?msie?10.0;?windows?nt?6.1;?wow64;?trident/6.0)" 192.168.18.138?-?-?[04/sep/2013:09:41:59?+0800]?"get?/?http/1.0"?200?23?"-"?"mozilla/5.0?(compatible;?msie?10.0;?windows?nt?6.1;?wow64;?trident/6.0)" 192.168.18.138?-?-?[04/sep/2013:09:42:00?+0800]?"get?/?http/1.0"?200?23?"-"?"mozilla/5.0?(compatible;?msie?10.0;?windows?nt?6.1;?wow64;?trident/6.0)" 192.168.18.138?-?-?[04/sep/2013:09:42:00?+0800]?"get?/?http/1.0"?200?23?"-"?"mozilla/5.0?(compatible;?msie?10.0;?windows?nt?6.1;?wow64;?trident/6.0)" 192.168.18.138?-?-?[04/sep/2013:09:42:00?+0800]?"get?/?http/1.0"?200?23?"-"?"mozilla/5.0?(compatible;?msie?10.0;?windows?nt?6.1;?wow64;?trident/6.0)" 192.168.18.138?-?-?[04/sep/2013:09:44:21?+0800]?"get?/?http/1.0"?200?23?"-"?"mozilla/5.0?(compatible;?msie?10.0;?windows?nt?6.1;?wow64;?trident/6.0)" 192.168.18.138?-?-?[04/sep/2013:09:44:22?+0800]?"get?/?http/1.0"?200?23?"-"?"mozilla/5.0?(compatible;?msie?10.0;?windows?nt?6.1;?wow64;?trident/6.0)" 192.168.18.138?-?-?[04/sep/2013:09:44:22?+0800]?"get?/?http/1.0"?200?23?"-"?"mozilla/5.0?(compatible;?msie?10.0;?windows?nt?6.1;?wow64;?trident/6.0)"

web2:

先修改一下,web服務器記錄日志的格式。

[root@web2?~]#?vim?/etc/httpd/conf/httpd.conf logformat?"%{x-real-ip}i?%l?%u?%t?"%r"?%>s?%b?"%{referer}i"?"%{user-agent}i""?combined [root@web2?~]#?service?httpd?restart 停止?httpd:????????????????????????[確定] 正在啟動?httpd:??????????????????????[確定]

接著,再訪問多次,繼續(xù)查看日志。

[root@web2?~]#?tail?/var/log/httpd/access_log 192.168.18.138?-?-?[04/sep/2013:09:50:28?+0800]?"get?/?http/1.0"?200?23?"-"?"mozilla/5.0?(compatible;?msie?10.0;?windows?nt?6.1;?wow64;?trident/6.0)" 192.168.18.138?-?-?[04/sep/2013:09:50:28?+0800]?"get?/?http/1.0"?200?23?"-"?"mozilla/5.0?(compatible;?msie?10.0;?windows?nt?6.1;?wow64;?trident/6.0)" 192.168.18.138?-?-?[04/sep/2013:09:50:28?+0800]?"get?/?http/1.0"?200?23?"-"?"mozilla/5.0?(compatible;?msie?10.0;?windows?nt?6.1;?wow64;?trident/6.0)" 192.168.18.138?-?-?[04/sep/2013:09:50:28?+0800]?"get?/?http/1.0"?200?23?"-"?"mozilla/5.0?(compatible;?msie?10.0;?windows?nt?6.1;?wow64;?trident/6.0)" 192.168.18.138?-?-?[04/sep/2013:09:50:28?+0800]?"get?/?http/1.0"?200?23?"-"?"mozilla/5.0?(compatible;?msie?10.0;?windows?nt?6.1;?wow64;?trident/6.0)" 192.168.18.138?-?-?[04/sep/2013:09:50:28?+0800]?"get?/?http/1.0"?200?23?"-"?"mozilla/5.0?(compatible;?msie?10.0;?windows?nt?6.1;?wow64;?trident/6.0)" 192.168.18.138?-?-?[04/sep/2013:09:50:28?+0800]?"get?/?http/1.0"?200?23?"-"?"mozilla/5.0?(compatible;?msie?10.0;?windows?nt?6.1;?wow64;?trident/6.0)" 192.168.18.138?-?-?[04/sep/2013:09:50:28?+0800]?"get?/?http/1.0"?200?23?"-"?"mozilla/5.0?(compatible;?msie?10.0;?windows?nt?6.1;?wow64;?trident/6.0)" 192.168.18.138?-?-?[04/sep/2013:09:50:29?+0800]?"get?/?http/1.0"?200?23?"-"?"mozilla/5.0?(compatible;?msie?10.0;?windows?nt?6.1;?wow64;?trident/6.0)" 192.168.18.138?-?-?[04/sep/2013:09:50:29?+0800]?"get?/?http/1.0"?200?23?"-"?"mozilla/5.0?(compatible;?msie?10.0;?windows?nt?6.1;?wow64;?trident/6.0)"

注,大家可以看到,兩臺服務器日志都記錄是192.168.18.138訪問的日志,也說明了負載均衡配置成功。

9.配置nginx進行健康狀態(tài)檢查

  • max_fails,允許請求失敗的次數(shù),默認為1。如果次數(shù)超過最大次數(shù),則會返回由 proxy_next_upstream 模塊定義的錯誤。

  • 在經(jīng)歷了最大允許失敗次數(shù)(max_fails)后,服務會暫停一段時間(fail_timeout)。使用max_fails和fail_timeout可以進行健康狀態(tài)檢查。

[root@nginx?~]#?vim?/etc/nginx/nginx.conf upstream?webservers?{ ????server?192.168.18.201?weight=1?max_fails=2?fail_timeout=2; ????server?192.168.18.202?weight=1?max_fails=2?fail_timeout=2; ??}

10.重新加載一下配置文件

[root@nginx?~]#?service?nginx?reload nginx:?the?configuration?file?/etc/nginx/nginx.conf?syntax?is?ok nginx:?configuration?file?/etc/nginx/nginx.conf?test?is?successful 重新載入?nginx:??????????????????????[確定]

11.停止服務器并測試

先停止web1,進行測試。 [root@web1?~]#?service?httpd?stop 停止?httpd:????????????????????????[確定]

nginx負載均衡實例分析

注,大家可以看到,現(xiàn)在只能訪問web2,再重新啟動web1,再次訪問一下。

[root@web1?~]#?service?httpd?start 正在啟動?httpd:??????????????????????[確定]

nginx負載均衡實例分析

nginx負載均衡實例分析

注,大家可以看到,現(xiàn)在又可以重新訪問,說明nginx的健康狀態(tài)查檢配置成功。但大家想一下,如果不幸的是所有服務器都不能提供服務了怎么辦,用戶打開頁面就會出現(xiàn)出錯頁面,那么會帶來用戶體驗的降低,所以我們能不能像配置lvs是配置sorry_server呢,答案是可以的,但這里不是配置sorry_server而是配置backup。

12.配置backup服務器

[root@nginx?~]#?vim?/etc/nginx/nginx.conf server?{ ????????listen?8080; ????????server_name?localhost; ????????root?/data/www/errorpage; ????????index?index.html; ????} upstream?webservers?{ ????server?192.168.18.201?weight=1?max_fails=2?fail_timeout=2; ????server?192.168.18.202?weight=1?max_fails=2?fail_timeout=2; ????server?127.0.0.1:8080?backup; ??} [root@nginx?~]#?mkdir?-pv?/data/www/errorpage [root@nginx?errorpage]#?cat?index.html <h1>sorry......</h1>

13.重新加載配置文件

[root@nginx?errorpage]#?service?nginx?reload nginx:?the?configuration?file?/etc/nginx/nginx.conf?syntax?is?ok nginx:?configuration?file?/etc/nginx/nginx.conf?test?is?successful 重新載入?nginx:??????????????????????[確定]

14.關閉web服務器并進行測試

[root@web1?~]#?service?httpd?stop 停止?httpd:????????????????????????[確定] [root@web2?~]#?service?httpd?stop 停止?httpd:????????????????????????[確定]

nginx負載均衡實例分析

注,大家可以看到,當所有服務器都不能工作時,就會啟動備份服務器。好了,backup服務器就配置到這里,下面我們來配置ip_hash負載均衡。

15.配置ip_hash負載均衡

ip_hash,每個請求按訪問ip的hash結果分配,這樣來自同一個ip的訪客固定訪問一個后端服務器,有效解決了動態(tài)網(wǎng)頁存在的session共享問題。(一般電子商務網(wǎng)站用的比較多)

[root@nginx?~]#?vim?/etc/nginx/nginx.conf upstream?webservers?{ ????ip_hash; ????server?192.168.18.201?weight=1?max_fails=2?fail_timeout=2; ????server?192.168.18.202?weight=1?max_fails=2?fail_timeout=2; ????#server?127.0.0.1:8080?backup; ??}

注,當負載調度算法為ip_hash時,后端服務器在負載均衡調度中的狀態(tài)不能有backup。(有人可能會問,為什么呢?大家想啊,如果負載均衡把你分配到backup服務器上,你能訪問到頁面嗎?不能,所以了不能配置backup服務器)

16.重新加載一下服務器

[root@nginx?~]#?service?nginx?reload nginx:?the?configuration?file?/etc/nginx/nginx.conf?syntax?is?ok nginx:?configuration?file?/etc/nginx/nginx.conf?test?is?successful 重新載入?nginx:??????????????????????[確定]

17.測試一下

nginx負載均衡實例分析

注,大家可以看到,你不斷的刷新頁面一直會顯示的民web2,說明ip_hash負載均衡配置成功。下面我們來統(tǒng)計一下web2的訪問連接數(shù)。

18.統(tǒng)計web2的訪問連接數(shù)

[root@web2?~]#?netstat?-an?|?grep?:80?|?wc?-l 304

注,你不斷的刷新,連接數(shù)會越來越多。

? 版權聲明
THE END
喜歡就支持一下吧
點贊11 分享