nginx通過nginx_upstream_check_module實現(xiàn)后端健康檢查
1、簡介說明
nginx是常用的反向代理和負載均衡服務,具有強大并發(fā)能力、穩(wěn)定性、豐富的功能集、低資源的消耗。
nginx自身是沒有針對后端節(jié)點健康檢查的,但是可以通過默認自帶的ngx_http_proxy_module 模塊和ngx_http_upstream_module模塊中的相關(guān)指令來完成當后端節(jié)點出現(xiàn)故障時,自動切換到健康節(jié)點來提供訪問。
nginx的健康檢查有兩種,一種是被動健康檢查,也就是nginx自帶健康檢查模塊ngx_http_upstream_module,另一種就是主動健康檢查,使用第三方模塊nginx_upstream_check_module。
nginx被動健康檢查的缺點:
- nginx只有被訪問時,才會發(fā)起對后端節(jié)點探測。如果本次請求中,節(jié)點正好出現(xiàn)故障,nginx依然會將請求轉(zhuǎn)交給故障的節(jié)點,然后再轉(zhuǎn)交給健康的節(jié)點處理。所以不會影響到這次請求的正常進行。由于多了一次轉(zhuǎn)發(fā),會影響效率。
- 無法做到預警。
nginx主動健康檢查
- 淘寶開發(fā)的tengine自帶心跳檢測模塊,若健康檢查包類型為http,在開啟健康檢查功能后,nginx會根據(jù)設置的間隔向后端服務器端口發(fā)送健康檢查包,并根據(jù)期望的HTTP狀態(tài)碼來判斷服務是否健康。后端節(jié)點不可用,則請求不會轉(zhuǎn)發(fā)到故障節(jié)點。
- 故障節(jié)點恢復后,請求正常轉(zhuǎn)發(fā)
nginx_upstream_check_module是一個專門提供負載均衡器內(nèi)節(jié)點的健康檢查的,這個是淘寶技術(shù)團隊開發(fā)的 nginx 模塊 ,通過它可以用來檢測后端 realserver 的健康狀態(tài)。如果后端 realserver 不可用,則所以的請求就不會轉(zhuǎn)發(fā)到該節(jié)點上。
淘寶的 tengine 自帶了該模塊,官方地址:http://tengine.taobao.org。如果是 nginx,可以通過補丁的方式來添加該模塊到 nginx 中(https://github.com/yaoweibin/nginx_upstream_check_module)。
2、安裝配置
2.1 下載nginx 和 nginx_upstream_check_module
# cd /usr/local/src # wget https://nginx.org/download/nginx-1.20.2.tar.gz # tar zxf nginx-1.20.2.tar.gz # git clone https://github.com/yaoweibin/nginx_upstream_check_module.git # ls -l total 1044 drwxr-xr-x 8 1001 1001 158 Nov 16 2021 nginx-1.20.2 -rw-r--r-- 1 root root 1062124 Nov 16 2021 nginx-1.20.2.tar.gz drwxr-xr-x 7 root root 4096 Jul 20 23:50 nginx_upstream_check_module
2.2 為nginx打補丁并編譯安裝
# cd nginx-1.20.2 # patch -p1 < /usr/local/src/nginx_upstream_check_module/check_1.20.1+.patch patching file src/http/modules/ngx_http_upstream_hash_module.c 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 # ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_geoip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-pcre --with-pcre-jit --with-stream_geoip_module --add-module=/usr/local/src/nginx_upstream_check_module # make && make install
2.3 配置案例及效果
# cat conf/conf.d/nginx_upstream_check.conf
upstream cluster{
server 192.168.100.210:9091;
server 192.168.100.210:9092;
check interval=3000 rise=2 fall=2 timeout=3000 type=http;
check_http_send "GET /ops/v1/check HTTP/1.0\r\n\r\n ";
check_http_expect_alive http_2xx http_3xx;
}
server {
listen 8888;
server_name localhost;
#charset koi8-r;
access_log logs/nginx_upstream_check.log main;
location / {
root html;
index index.html;
}
location ^~ /nginxServer/ {
proxy_pass http://cluster/;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass_request_body on;
proxy_set_header Cookie $http_cookie;
real_ip_header X-Real-IP;
}
location /nginx_status {
check_status;
access_log off;
}
}


2.4 語法及指令介紹
check interval=milliseconds [fall=count] [rise=count] [timeout=milliseconds] [default_down=true|false] [type=tcp|http|ssl_hello|mysql|ajp] [port=check_port]
- interval: 向后端發(fā)送的健康檢查包的間隔,單位為毫秒
- rise: 如果連續(xù)成功次數(shù)達到rise_count,服務器就被認為是up
- fall: 如果連續(xù)失敗次數(shù)達到fall_count,服務器就被認為是down
- timeout: 后端健康請求的超時時間,單位為毫秒
- type: 健康檢查包的類型,支持類型如下:
tcp:簡單的tcp連接,如果連接成功,就說明后端正常
ssl_hello:發(fā)送一個初始的SSL hello包并接受服務器的SSL hello包
http:發(fā)送HTTP請求,通過后端的回復包的狀態(tài)來判斷后端是否存活
mysql: 向mysql服務器連接,通過接收服務器的greeting包來判斷后端是否存活
ajp:向后端發(fā)送AJP協(xié)議的Cping包,通過接收Cpong包來判斷后端是否存活
- default_down: 設定初始時服務器的狀態(tài),如果是true,就說明默認是down的,如果是false,就是up的。默認值是true,也就是一開始服務器認為是不可用,要等健康檢查包達到一定成功次數(shù)以后才會被認為是健康的
- port: 指定后端服務器的檢查端口。你可以指定不同于真實服務的后端服務器的端口,比如后端提供的是443端口的應用,你可以去檢查80端口的狀態(tài)來判斷后端健康狀況。默認是0,表示跟后端server提供真實服務的端口一樣
2.5 check_http_send功能
用法:check_http_send "GET / HTTP/1.0\r\n\r\n"
默認值: "GET / HTTP/1.0\r\n\r\n"
位置:upstream塊
說明:http://ip:port/做健康檢測
2.6 監(jiān)控
You can specify the default display format. The formats can be `html`,
`csv` or `json`. The default type is `html`. It also supports to specify
the format by the request argument. Suppose your `check_status` location
is '/status', the argument of `format` can change the display page's
format. You can do like this:
/status?format=html
/status?format=csv
/status?format=json
At present, you can fetch the list of servers with the same status by
the argument of `status`. For example:
/status?format=html&status=down
/status?format=csv&status=up到此這篇關(guān)于nginx通過nginx_upstream_check_module實現(xiàn)后端健康檢查的文章就介紹到這了,更多相關(guān)nginx 后端健康檢查內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
nginx如何通過proxy_pass設置反向代理,隱藏端口號
這篇文章主要介紹了nginx如何通過proxy_pass設置反向代理,隱藏端口號方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01

