一、ngx_http_upstream_module調(diào)度模塊
- 把后端服務(wù)器組成服務(wù)器組,然后調(diào)度,模塊用于定義可由proxy_pass, fastcgi_pass, uwsgi_pass, scgi_pass, and memcached_pass 指令引用的服務(wù)器組
1、 upstream name { ... }
定義后端服務(wù)器組;引入一個新的上下文;只能用于http{}上下文中;
默認(rèn)的調(diào)度方法是wrr;
2、 server address [parameters];
- 定義服務(wù)器地址和相關(guān)的參數(shù);
(1)地址格式:
IP[:PORT]
HOSTNAME[:PORT]
unix:/PATH/TO/SOME_SOCK_FILE
(2) 參數(shù):
- weight=number 權(quán)重,默認(rèn)為1;
- max_fails=number 失敗嘗試的最大次數(shù);
- fail_timeout=time 設(shè)置服務(wù)器為不可用狀態(tài)的超時(shí)時(shí)長;
- backup 把服務(wù)器標(biāo)記為“備用”狀態(tài);
- down 手動標(biāo)記其為不可用;
(3) least_conn;
最少連接調(diào)度算法;當(dāng)server擁有不同的權(quán)重時(shí)為wlc;當(dāng)所有后端主機(jī)的連接數(shù)相同時(shí),則使用wrr進(jìn)行調(diào)度;
(4) least_time header | last_byte;
最短平均響應(yīng)時(shí)長和最少連接;
header:response_header;
last_byte: full_response;
僅Nginx Plus有效;
(5) ip_hash;
源地址hash算法;能夠?qū)碜酝粋€源IP地址的請求始終發(fā)往同一個upstream server;
(6) hash key [consistent];
基于指定的key的hash表實(shí)現(xiàn)請求調(diào)度,此處的key可以文本、變量或二者的組合;
consistent:參數(shù),指定使用一致性hash算法;
示例:
hash $request_uri consistent
hash $remote_addr
hash $cookie_name
(7) keepalive connections;
可使用長連接的連接數(shù)量;
(8) health_check [parameters];
定義對后端主機(jī)的健康狀態(tài)檢測機(jī)制;只能用于location上下文;
可用參數(shù):
interval=time:檢測頻率,默認(rèn)為每隔5秒鐘;
fails=number:判斷服務(wù)器狀態(tài)轉(zhuǎn)為失敗需要檢測的次數(shù);
passes=number:判斷服務(wù)器狀態(tài)轉(zhuǎn)為成功需要檢測的次數(shù);
uri=uri:判斷其健康與否時(shí)使用的uri;
match=name:基于指定的match來衡量檢測結(jié)果的成敗;
port=number:使用獨(dú)立的端口進(jìn)行檢測;
僅Nginx Plus有效;
(9) match name { ... }
Defines the named test set used to verify responses to health check requests.
定義衡量某檢測結(jié)果是否為成功的衡量機(jī)制;
專用指令:
status:期望的響應(yīng)碼;
status CODE
status ! CODE
...
header:基于響應(yīng)報(bào)文的首部進(jìn)行判斷
header HEADER=VALUE
header HEADER ~ VALUE
...
body:基于響應(yīng)報(bào)文的內(nèi)容進(jìn)行判斷
body ~ "PATTERN"
body !~ "PATTERN"
二、基于服務(wù)器組調(diào)度示例
- 調(diào)度VS服務(wù)設(shè)置
[root@vs conf.d]# vim ilinux.conf#編輯
server{
listen 80; #監(jiān)聽80端口
server_name www.linux.io;#服務(wù)名稱
location / {
root /data/nginx/html;
proxy_pass http://webservs;#代理到web組服務(wù)器
}
}
[root@vs conf.d]# vim ../nginx.conf
http {
.........
upstream websrvs{ #設(shè)置代理組
ip_hash; #綁定來自同一個客戶端ip請求始終分發(fā)到同一主機(jī)
hash $request_uri consistent;#綁定同一uri請求始終分發(fā)到同一主機(jī),一致性哈希環(huán)算法
server 192.168.1.11:80 weight=2;#加權(quán)輪詢
server 192.168.1.12:80 fail_timeout=1 max_fails=3;#檢測失敗超時(shí)1秒,最大檢測失敗3次
server 127.0.0.1:80 backup;#當(dāng)后端服務(wù)器全部離線,啟用備用服務(wù)器
}
客戶端測試:
輪詢調(diào)度:
[root@mysql ~]# curl http://www.ilinux.io/index.html
<h1>Upstream Server1</h1>
[root@mysql ~]# curl http://www.ilinux.io/index.html
<h1>Upstream Server2</h1>
[root@mysql ~]# curl http://www.ilinux.io/index.html
<h1>Upstream Server1</h1>
[root@mysql ~]# curl http://www.ilinux.io/index.html
<h1>Upstream Server2</h1>
加權(quán)輪詢調(diào)度:
[root@mysql ~]# for i in {1..10};do curl http://www.ilinux.io/index.html;done
<h1>Upstream Server1</h1>
<h1>Upstream Server1</h1>
<h1>Upstream Server2</h1>
<h1>Upstream Server1</h1>
<h1>Upstream Server1</h1>
<h1>Upstream Server2</h1>
<h1>Upstream Server1</h1>
<h1>Upstream Server1</h1>
<h1>Upstream Server2</h1>
<h1>Upstream Server1</h1>