1.被動健康檢查
Nginx自帶有健康檢查模塊:ngx_http_upstream_module,可以做到基本的健康檢查,配置如下:
upstream cluster{ server 172.16.0.23:80 max_fails=1 fail_timeout=10s; server 172.16.0.24:80 max_fails=1 fail_timeout=10s;
# max_fails=1和fail_timeout=10s 表示在單位周期為10s鍾內,中達到1次連接失敗,那么接將把節點標記為不可用,並等待下一個周期(同樣時常為fail_timeout)再一次去請求,判斷是否連接是否成功。
# fail_timeout為10s,max_fails為1次。
} server { listen 80; server_name xxxxxxx.com; location / { proxy_pass http://cluster; } }
Nginx只有當有訪問時后,才發起對后端節點探測。如果本次請求中,節點正好出現故障,Nginx依然將請求轉交給故障的節點,然后再轉交給健康的節點處理。所以不會影響到這次請求的正常進行。但是會影響效率,因為多了一次轉發,而且自帶模塊無法做到預警。
2.主動健康檢查(需使用第三方模塊)
主動地健康檢查,nignx定時主動地去ping后端的服務列表,當發現某服務出現異常時,把該服務從健康列表中移除,當發現某服務恢復時,又能夠將該服務加回健康列表中。淘寶有一個開源的實現nginx_upstream_check_module模塊
官網:http://tengine.taobao.org/document_cn/http_upstream_check_cn.html
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; check_http_send "HEAD / HTTP/1.0\r\n\r\n"; 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; } } }
3.集成第三方模塊部署
3.1、下載nginx_upstream_check_module模塊
#進入nginx安裝目錄 cd /usr/local/nginx #下載nginx_upstream_check_module模塊 wget https://codeload.github.com/yaoweibin/nginx_upstream_check_module/zip/master #wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/master.zip #解壓 unzip master cd nginx-1.12 # 進入nginx的源碼目錄 # -p0,是“當前路徑” -p1,是“上一級路徑” patch -p1 < ../nginx_upstream_check_module-master/check_1.11.5+.patch #nginx -V 可以查看原有配置 輸出 ./configure --prefix=/usr/local/nginx #增加upstream_check模塊 ./configure --prefix=/usr/local/nginx --add-module=../nginx_upstream_check_module-master make /usr/local/nginx/sbin/nginx -t # 檢查下是否有問題 注意 check版本和Nginx版本要求有限制 1.12以上版本的nginx,補丁為check_1.11.5+.patch 具體參考github https://github.com/yaoweibin/nginx_upstream_check_module
3.2.修改配置文件,讓nginx_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; check_http_send "HEAD / HTTP/1.0\r\n\r\n"; 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; } } }
3.3重載nginx
訪問http://nginx/nstatus
人為把其中的一個節點關掉刷新http://nginx/nstatus
udp反向代理時健康檢查的問題,另一位大神在上面nginx_upstream_check_module的基礎上作了修改,實現了在第4層的代理tcp和udp時的健康檢查。