Tengine健康檢查模塊
doc :http://tengine.taobao.org/document_cn/http_upstream_check_cn.html
ngx_http_upstream_check_module
配置栗子
http {
upstream cluster1 {
# simple round-robin
server 192.168.0.1:80;
server 192.168.0.2:80;
check interval=3000 rise=2 fall=5 timeout=1000 type=http;
# 這里的請求頭使用HEAD 目的是為了請求短 占用資源少
# HEAD后面的/代表檢查根目錄
check_http_send "HEAD / HTTP/1.0\r\n\r\n";
# 健康檢查標識的狀態(tài)碼 表示返回2xx 3xx表示正常
check_http_expect_alive http_2xx http_3xx;
}
upstream cluster2 {
# simple round-robin
server 192.168.0.3:80;
server 192.168.0.4:80;
check interval=3000 rise=2 fall=5 timeout=1000 type=http;
check_keepalive_requests 100;
check_http_send "HEAD / HTTP/1.1\r\nConnection: keep-alive\r\n\r\n";
check_http_expect_alive http_2xx http_3xx;
}
server {
listen 80;
location /1 {
proxy_pass http://cluster1;
}
location /2 {
proxy_pass http://cluster2;
}
location /status {
check_status;
access_log off;
allow SOME.IP.ADD.RESS;
deny all;
}
}
}
名次解釋
interval:向后端發(fā)送的健康檢查包的間隔。
fall(fall_count): 如果連續(xù)失敗次數達到fall_count,服務器就被認為是down。
rise(rise_count): 如果連續(xù)成功次數達到rise_count,服務器就被認為是up。
timeout: 后端健康請求的超時時間。
default_down: 設定初始時服務器的狀態(tài),如果是true,就說明默認是down的,如果是false,就是up的。默認值是true,也就是一開始服務器認為是不可用,要等健康檢查包達到一定成功次數以后才會被認為是健康的。
type:健康檢查包的類型,現在支持以下多種類型
tcp:簡單的tcp連接,如果連接成功,就說明后端正常。
ssl_hello:發(fā)送一個初始的SSL hello包并接受服務器的SSL hello包。
http:發(fā)送HTTP請求,通過后端的回復包的狀態(tài)來判斷后端是否存活。
mysql: 向mysql服務器連接,通過接收服務器的greeting包來判斷后端是否存活。
ajp:向后端發(fā)送AJP協議的Cping包,通過接收Cpong包來判斷后端是否存活。
port: 指定后端服務器的檢查端口。你可以指定不同于真實服務的后端服務器的端口,比如后端提供的是443端口的應用,你可以去檢查80端口的狀態(tài)來判斷后端健康狀況。默認是0,表示跟后端server提供真實服務的端口一樣。該選項出現于Tengine-1.4.0。
我們自己的栗子
tengine新增健康檢查模塊
配置一個status的location
location /status {
check_status;
}
在upstream配置如下
check interval=3000 rise=2 fall=5 timeout=1000 type=http;
check_http_send "HEAD / HTTP/1.0\r\n\r\n";
check_http_expect_alive http_2xx http_3xx;
- 完整配置
# 反向代理配置
44 upstream proxyconf {
45 server 192.168.1.63:8080;
46 server 192.168.1.62:8080;
47 check interval=3000 rise=2 fall=5 timeout=1000 type=http;
48 check_http_send "HEAD / HTTP/1.0\r\n\r\n";
49 check_http_expect_alive http_2xx http_3xx;
50 }
51 # 虛主機配置
52 server {
53 listen 80;
54 server_name localhost;
55 location / {
56 root html;
57 index index.html index.htm;
58 proxy_pass http://proxyconf;
59 }
65 location /status {
66 check_status;
67 }
68 }
狀態(tài)監(jiān)控圖:

圖中紅色的代表出問題的服務器