首頁 > 運維 > Nginx > 主體

nginx負載平衡執行個體分析

WBOY
發布: 2023-05-21 08:01:32
轉載
1051 人瀏覽過

nginx之負載平衡

#註,大家可以看到,由於我們網站是發展初期,nginx只代理了後端一台伺服器,但由於我們網站名聲大漲訪問的人越來越多一台伺服器實在是頂不住,於是我們加了多台伺服器,那麼多台伺服器又怎麼配置代理呢,我們這裡以兩台伺服器為案例,為大家做演示。

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模組,這個模組透過一個簡單的調度演算法來實現客戶端ip到後端伺服器的負載平衡。在上面的設定中,透過upstream指令指定了一個負載平衡器的名稱test.net。這個名稱可以任意指定,後面需要用到的地方直接呼叫即可。

2.upstream 支援的負載平衡演算法

nginx的負載平衡模組目前支援4種調度演算法,以下進行分別介紹,其中後兩項屬於第三方調度演算法。  

  • 輪詢(預設)。每個請求依時間順序逐一分配到不同的後端伺服器,如果後端某台伺服器宕機,故障系統會自動剔除,使用戶存取不受影響。 weight 指定輪詢權值,weight值越大,分配到的存取機率越高,主要用於後端每個伺服器效能不均的情況。

  • ip_hash。每個請求按訪問ip的hash結果分配,這樣來自同一個ip的訪客固定訪問一個後端伺服器,有效解決了動態網頁存在的session共享問題。

  • fair。這是比上面兩個更聰明的負載平衡演算法。此種演算法可以依據頁面大小和載入時間長短智慧地進行負載平衡,也就是根據後端伺服器的回應時間來分配請求,回應時間短的優先分配。 nginx本身是不支援fair的,如果需要使用這種調度演算法,必須下載nginx的upstream_fair模組。

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

3.upstream 支援的狀態參數

#在http upstream模組中,可以透過server指令指定後端伺服器的ip位址和端口,同時也可以設定每個後端伺服器在負載平衡調度中的狀態。常用的狀態有:     

  • #down,表示目前的server暫時不參與負載平衡。

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

  • max_fails,允許請求失敗的次數,預設為1。如果次數超過最大次數,則會傳回 proxy_next_upstream 模組定義的錯誤。

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

註,當負載調度演算法為ip_hash時,後端伺服器在負載平衡調度中的狀態不能是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負載平衡執行個體分析

註,大家可以不斷的刷新瀏覽的內容,可以發現web1與web2是交替出現的,達到了負載平衡的效果。

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:

#先修改一下,網頁伺服器記錄日誌的格式。

[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:                      [确定]
登入後複製

接著,再訪問多次,繼續查看日誌。

[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進行健康狀態檢查

  • #max_fails,允許請求失敗的次數,預設為1。如果次數超過最大次數,則會傳回 proxy_next_upstream 模組定義的錯誤。

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

[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負載平衡執行個體分析

注,大家可以看到,现在只能访问web2,再重新启动web1,再次访问一下。

[root@web1 ~]# service httpd start
正在启动 httpd:                      [确定]
登入後複製

nginx負載平衡執行個體分析

nginx負載平衡執行個體分析

注,大家可以看到,现在又可以重新访问,说明nginx的健康状态查检配置成功。但大家想一下,如果不幸的是所有服务器都不能提供服务了怎么办,用户打开页面就会出现出错页面,那么会带来用户体验的降低,所以我们能不能像配置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的访客固定访问一个后端服务器,有效解决了动态网页存在的session共享问题。(一般电子商务网站用的比较多)

[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时,后端服务器在负载均衡调度中的状态不能有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负载均衡配置成功。下面我们来统计一下web2的访问连接数。

18.统计web2的访问连接数

[root@web2 ~]# netstat -an | grep :80 | wc -l
304
登入後複製

注,你不断的刷新,连接数会越来越多。

以上是nginx負載平衡執行個體分析的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:yisu.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!