nginx健康檢查 nginx_upstream_check_modules

檢查後端狀態 後端如果出現故障或down機,如何不讓他請求故障服務器,有時候存在down了也還繼續請求的現象

採用第三方模塊:

nginx_upstream_check_module (淘寶)
https://github.com/yaoweibin/nginx_upstream_check_module
healthcheck_nginx_upstreams (自帶)
https://github.com/cep21/healthcheck_nginx_upstreams

下載:
wget https://codeload.github.com/yaoweibin/nginx_upstream_check_module/zip/master

導入步驟:

unzip ./nginx_upstream_check_module-master.zip
注意是將補丁打入Nginx源碼,不是Nginx的安裝路徑:
根據版本,選擇check版本

導入前提示:

If you use nginx-1.2.1 or nginx-1.3.0, the nginx upstream round robin
module changed greatly. You should use the patch named
'check_1.2.1.patch'.
If you use nginx-1.2.2+ or nginx-1.3.1+, It added the upstream
least_conn module. You should use the patch named 'check_1.2.2+.patch'.
If you use nginx-1.2.6+ or nginx-1.3.9+, It adjusted the round robin
module. You should use the patch named 'check_1.2.6+.patch'.
If you use nginx-1.5.12+, You should use the patch named
'check_1.5.12+.patch'.
If you use nginx-1.7.2+, You should use the patch named
'check_1.7.2+.patch'.

正式導入

# yum install patch -y
# cd ./nginx-1.13.9
# sed -i -e 's/1.6.2/2.0/g' -e 's/nginx\//LXS/g' -e 's/"NGINX"/"LXS"/g' src/core/nginx.h 
# patch -p1 < ../nginx_upstream_check_module-master/check_1.12.1+.patch 

成功:輸入信息,如果出現FAIL表示錯誤

patching file src/http/modules/ngx_http_upstream_ip_hash_module.c
patching file src/http/modules/ngx_http_upstream_least_conn_module.c
patching file src/http/ngx_http_upstream_round_robin.c
patching file src/http/ngx_http_upstream_round_robin.h

編譯nginx:

./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_ssl_module    --add-module=../nginx_upstream_check_module-master/

配置負載均衡:

upstream cluster {
# simple round-robin
server 192.168.2.128:80;
server 192.168.2.129:80;
check interval=5000 rise=1 fall=3 timeout=4000;
#check interval=3000 rise=2 fall=5 timeout=1000 type=ssl_hello;
#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;
}

上面的代碼中,check部分就是調用nginx_upstream_check_module模塊的語法:

check interval=milliseconds [fall=count] [rise=count]
[timeout=milliseconds] [default_down=true|false]
[type=tcp|http|ssl_hello|mysql|ajp|fastcgi]

interval:必要參數,檢查請求的間隔時間。
fall:當檢查失敗次數超過了fall,這個服務節點就變成down狀態。
rise:當檢查成功的次數超過了rise,這個服務節點又會變成up狀態。
timeout:請求超時時間,超過等待時間後,這次檢查就算失敗。    default_down:後端服務器的初始狀態。默認情況下,檢查功能在Nginx啓動的時候將會把所有後端節點的狀態置爲down,檢查成功後,在置爲up。
type:這是檢查通信的協議類型,默認爲http。以上類型是檢查功能所支持的所有協議類型。

加入健康檢查狀態:

    location /nstatus {
        check_status;
        access_log off;
        #allow SOME.IP.ADD.RESS;
        #deny all;
        }

nginx健康檢查  nginx_upstream_check_modules

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章