注,大家可以看到,由于我們網(wǎng)站是發(fā)展早期,nginx只代理了后端1臺(tái)服務(wù)器,但由于我們網(wǎng)站名望大漲訪問的人愈來愈多1臺(tái)服務(wù)器實(shí)在是頂不住,因而我們加了多臺(tái)服務(wù)器,那末多臺(tái)服務(wù)器又怎樣配置代理呢,我們這里以兩臺(tái)服務(wù)器為案例,為大家做演示。
1.upstream 負(fù)載均衡模塊說明
案例:
下面設(shè)定負(fù)載均衡的服務(wù)器列表。
1
2
3
4
5
6
7
8
9
10
11
12
|
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模塊,這個(gè)模塊通過1個(gè)簡單的調(diào)度算法來實(shí)現(xiàn)客戶端IP到后端服務(wù)器的負(fù)載均衡。在上面的設(shè)定中,通過upstream指令指定了1個(gè)負(fù)載均衡器的名稱test.net。這個(gè)名稱可以任意指定,在后面需要用到的地方直接調(diào)用便可。
2.upstream 支持的負(fù)載均衡算法
Nginx的負(fù)載均衡模塊目前支持4種調(diào)度算法,下面進(jìn)行分別介紹,其中后兩項(xiàng)屬于第3方調(diào)度算法。
輪詢(默許)。每一個(gè)要求按時(shí)間順序逐1分配到不同的后端服務(wù)器,如果后端某臺(tái)服務(wù)器宕機(jī),故障系統(tǒng)被自動(dòng)剔除,使用戶訪問不受影響。Weight 指定輪詢權(quán)值,Weight值越大,分配到的訪問機(jī)率越高,主要用于后端每一個(gè)服務(wù)器性能不均的情況下。
ip_hash。每一個(gè)要求按訪問IP的hash結(jié)果分配,這樣來自同1個(gè)IP的訪客固定訪問1個(gè)后端服務(wù)器,有效解決了動(dòng)態(tài)網(wǎng)頁存在的session同享問題。
fair。這是比上面兩個(gè)更加智能的負(fù)載均衡算法。此種算法可以根據(jù)頁面大小和加載時(shí)間長短智能地進(jìn)行負(fù)載均衡,也就是根據(jù)后端服務(wù)器的響應(yīng)時(shí)間來分配要求,響應(yīng)時(shí)間短的優(yōu)先分配。Nginx本身是不支持fair的,如果需要使用這類調(diào)度算法,必須下載Nginx的upstream_fair模塊。
url_hash。此方法按訪問url的hash結(jié)果來分配要求,使每一個(gè)url定向到同1個(gè)后端服務(wù)器,可以進(jìn)1步提高后端緩存服務(wù)器的效力。Nginx本身是不支持url_hash的,如果需要使用這類調(diào)度算法,必須安裝Nginx 的hash軟件包。
3.upstream 支持的狀態(tài)參數(shù)
在HTTP Upstream模塊中,可以通過server指令指定后端服務(wù)器的IP地址和端口,同時(shí)還可以設(shè)定每一個(gè)后端服務(wù)器在負(fù)載均衡調(diào)度中的狀態(tài)。經(jīng)常使用的狀態(tài)有:
down,表示當(dāng)前的server暫時(shí)不參與負(fù)載均衡。
backup,預(yù)留的備份機(jī)器。當(dāng)其他所有的非backup機(jī)器出現(xiàn)故障或忙的時(shí)候,才會(huì)要求backup機(jī)器,因此這臺(tái)機(jī)器的壓力最輕。
max_fails,允許要求失敗的次數(shù),默許為1。當(dāng)超過最大次數(shù)時(shí),返回proxy_next_upstream 模塊定義的毛病。
fail_timeout,在經(jīng)歷了max_fails次失敗后,暫停服務(wù)的時(shí)間。max_fails可以和fail_timeout1起使用。
注,當(dāng)負(fù)載調(diào)度算法為ip_hash時(shí),后端服務(wù)器在負(fù)載均衡調(diào)度中的狀態(tài)不能是weight和backup。
4.實(shí)驗(yàn)拓?fù)?/span>
5.配置nginx負(fù)載均衡
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
[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; location / { proxy_pass http: //webservers ; proxy_set_header X-Real-IP $remote_addr; } } |
注,upstream是定義在server{ }以外的,不能定義在server{ }內(nèi)部。定義好upstream以后,用proxy_pass援用1下便可。
6.重新加載1下配置文件
1
2
3
4
|
[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.測(cè)試1下
注,大家可以不斷的刷新閱讀的內(nèi)容,可以發(fā)現(xiàn)web1與web2是交替出現(xiàn)的,到達(dá)了負(fù)載均衡的效果。
8.查看1下Web訪問服務(wù)器日志
Web1:
1
2
3
4
5
6
7
8
9
10
11
|
[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:
先修改1下,Web服務(wù)器記錄日志的格式。
1
2
3
4
5
|
[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: [肯定] 正在啟動(dòng) httpd: [肯定] |
接著,再訪問屢次,繼續(xù)查看日志。
1
2
3
4
5
6
7
8
9
10
11
|
[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)" |
注,大家可以看到,兩臺(tái)服務(wù)器日志都記錄是192.168.18.138訪問的日志,也說明了負(fù)載均衡配置成功。
9.配置nginx進(jìn)行健康狀態(tài)檢查
max_fails,允許要求失敗的次數(shù),默許為1。當(dāng)超過最大次數(shù)時(shí),返回proxy_next_upstream 模塊定義的毛病。
fail_timeout,在經(jīng)歷了max_fails次失敗后,暫停服務(wù)的時(shí)間。max_fails可以和fail_timeout1起使用,進(jìn)行健康狀態(tài)檢查。
1
2
3
4
5
|
[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.重新加載1下配置文件
1
2
3
4
|
[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.停止服務(wù)器并測(cè)試
1
2
3
|
先停止Web1,進(jìn)行測(cè)試。 [root@web1 ~] # service httpd stop 停止 httpd: [肯定] |
注,大家可以看到,現(xiàn)在只能訪問Web2,再重新啟動(dòng)Web1,再次訪問1下。
1
2
|
[root@web1 ~] # service httpd start 正在啟動(dòng) httpd: [肯定] |
注,大家可以看到,現(xiàn)在又可以重新訪問,說明nginx的健康狀態(tài)查檢配置成功。但大家想1下,如果不幸的是所有服務(wù)器都不能提供服務(wù)了怎樣辦,用戶打開頁面就會(huì)出現(xiàn)出錯(cuò)頁面,那末會(huì)帶來用戶體驗(yàn)的下降,所以我們能不能像配置LVS是配置sorry_server呢,答案是可以的,但這里不是配置sorry_server而是配置backup。
12.配置backup服務(wù)器
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
[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.重新加載配置文件
1
2
3
4
|
[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.關(guān)閉Web服務(wù)器并進(jìn)行測(cè)試