nginx通過nginx_upstream_check_module實(shí)現(xiàn)后端健康檢查
1、簡介說明
nginx是常用的反向代理和負(fù)載均衡服務(wù),具有強(qiáng)大并發(fā)能力、穩(wěn)定性、豐富的功能集、低資源的消耗。
nginx自身是沒有針對(duì)后端節(jié)點(diǎn)健康檢查的,但是可以通過默認(rèn)自帶的ngx_http_proxy_module 模塊和ngx_http_upstream_module模塊中的相關(guān)指令來完成當(dāng)后端節(jié)點(diǎn)出現(xiàn)故障時(shí),自動(dòng)切換到健康節(jié)點(diǎn)來提供訪問。
nginx的健康檢查有兩種,一種是被動(dòng)健康檢查,也就是nginx自帶健康檢查模塊ngx_http_upstream_module,另一種就是主動(dòng)健康檢查,使用第三方模塊nginx_upstream_check_module。
nginx被動(dòng)健康檢查的缺點(diǎn):
- nginx只有被訪問時(shí),才會(huì)發(fā)起對(duì)后端節(jié)點(diǎn)探測。如果本次請(qǐng)求中,節(jié)點(diǎn)正好出現(xiàn)故障,nginx依然會(huì)將請(qǐng)求轉(zhuǎn)交給故障的節(jié)點(diǎn),然后再轉(zhuǎn)交給健康的節(jié)點(diǎn)處理。所以不會(huì)影響到這次請(qǐng)求的正常進(jìn)行。由于多了一次轉(zhuǎn)發(fā),會(huì)影響效率。
- 無法做到預(yù)警。
nginx主動(dòng)健康檢查
- 淘寶開發(fā)的tengine自帶心跳檢測模塊,若健康檢查包類型為http,在開啟健康檢查功能后,nginx會(huì)根據(jù)設(shè)置的間隔向后端服務(wù)器端口發(fā)送健康檢查包,并根據(jù)期望的HTTP狀態(tài)碼來判斷服務(wù)是否健康。后端節(jié)點(diǎn)不可用,則請(qǐng)求不會(huì)轉(zhuǎn)發(fā)到故障節(jié)點(diǎn)。
- 故障節(jié)點(diǎn)恢復(fù)后,請(qǐng)求正常轉(zhuǎn)發(fā)
nginx_upstream_check_module是一個(gè)專門提供負(fù)載均衡器內(nèi)節(jié)點(diǎn)的健康檢查的,這個(gè)是淘寶技術(shù)團(tuán)隊(duì)開發(fā)的 nginx 模塊 ,通過它可以用來檢測后端 realserver 的健康狀態(tài)。如果后端 realserver 不可用,則所以的請(qǐng)求就不會(huì)轉(zhuǎn)發(fā)到該節(jié)點(diǎn)上。
淘寶的 tengine 自帶了該模塊,官方地址:http://tengine.taobao.org。如果是 nginx,可以通過補(bǔ)丁的方式來添加該模塊到 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打補(bǔ)丁并編譯安裝
# 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ù)達(dá)到rise_count,服務(wù)器就被認(rèn)為是up
- fall: 如果連續(xù)失敗次數(shù)達(dá)到fall_count,服務(wù)器就被認(rèn)為是down
- timeout: 后端健康請(qǐng)求的超時(shí)時(shí)間,單位為毫秒
- type: 健康檢查包的類型,支持類型如下:
tcp:簡單的tcp連接,如果連接成功,就說明后端正常
ssl_hello:發(fā)送一個(gè)初始的SSL hello包并接受服務(wù)器的SSL hello包
http:發(fā)送HTTP請(qǐng)求,通過后端的回復(fù)包的狀態(tài)來判斷后端是否存活
mysql: 向mysql服務(wù)器連接,通過接收服務(wù)器的greeting包來判斷后端是否存活
ajp:向后端發(fā)送AJP協(xié)議的Cping包,通過接收Cpong包來判斷后端是否存活
- default_down: 設(shè)定初始時(shí)服務(wù)器的狀態(tài),如果是true,就說明默認(rèn)是down的,如果是false,就是up的。默認(rèn)值是true,也就是一開始服務(wù)器認(rèn)為是不可用,要等健康檢查包達(dá)到一定成功次數(shù)以后才會(huì)被認(rèn)為是健康的
- port: 指定后端服務(wù)器的檢查端口。你可以指定不同于真實(shí)服務(wù)的后端服務(wù)器的端口,比如后端提供的是443端口的應(yīng)用,你可以去檢查80端口的狀態(tài)來判斷后端健康狀況。默認(rèn)是0,表示跟后端server提供真實(shí)服務(wù)的端口一樣
2.5 check_http_send功能
用法:check_http_send "GET / HTTP/1.0\r\n\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實(shí)現(xiàn)后端健康檢查的文章就介紹到這了,更多相關(guān)nginx 后端健康檢查內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Ubuntu?22.04.1?LTS?編譯安裝?nginx-1.22.1的配置過程
Ubuntu安裝Nginx有兩種方式,一種是通過命令的方式,這種方式安裝的Nginx版本低,之前漏掃掃出來Nginx版本低,需要升級(jí)所以現(xiàn)在用編譯的方式安裝版本高點(diǎn)的,本文介紹Ubuntu22.04.1?LTS編譯安裝nginx1.22.1的配置過程,本文給大家介紹的非常詳細(xì),需要的朋友參考下吧2024-01-01
Nginx的location的常見規(guī)則優(yōu)先級(jí)問題
Nginx是反向代理和負(fù)載均衡的首選工具,nginx的location配置有許多細(xì)節(jié)內(nèi)容在網(wǎng)上不容易找到資料,或者解釋不清。本文對(duì)Nginx location規(guī)則優(yōu)先級(jí)介紹,需要的朋友參考下吧2021-08-08
Centos基于Nginx搭建RTMP服務(wù)器的實(shí)現(xiàn)
本文主要介紹了Centos基于Nginx搭建RTMP服務(wù)器的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-07-07
iis+nginx實(shí)現(xiàn)負(fù)載均衡的詳細(xì)步驟
這篇文章主要為大家詳細(xì)介紹了iis+nginx實(shí)現(xiàn)負(fù)載均衡的詳細(xì)步驟 ,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
Nginx訪問FTP服務(wù)器文件的時(shí)效性/安全校驗(yàn)的方法
nginx的實(shí)現(xiàn)方式在校驗(yàn)失敗的時(shí)候頁面返回error image,跳轉(zhuǎn)的是420 error_page,成功的時(shí)候會(huì)訪問FTP文件服務(wù)器的路徑,反正圖片到頁面展示,這篇文章主要介紹了Nginx訪問FTP服務(wù)器文件的時(shí)效性/安全校驗(yàn),需要的朋友可以參考下2023-12-12
Nginx應(yīng)用之Location路由反向代理及重寫策略示例
本篇文章主要介紹了Nginx應(yīng)用之Location路由反向代理及重寫策略示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-02-02
Nginx 合并請(qǐng)求連接且加速網(wǎng)站訪問實(shí)例詳解
這篇文章主要介紹了Nginx 合并請(qǐng)求連接且加速網(wǎng)站訪問實(shí)例詳解,瀏覽器的并發(fā)請(qǐng)求數(shù)目限制是針對(duì)同一域名的,同一時(shí)間針對(duì)同一域名下的請(qǐng)求有一定數(shù)量限制,超過限制數(shù)目的請(qǐng)求會(huì)被阻塞,需要的朋友可以參考下2019-07-07

