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)文章
Windows設(shè)置nginx開機(jī)自啟動(dòng)的方法
這篇文章主要介紹了Windows設(shè)置nginx開機(jī)自啟動(dòng)的方法,通過兩種方式實(shí)現(xiàn)nginx的開機(jī)自啟動(dòng):winws和window計(jì)劃程序,每種方式給大家介紹的非常詳細(xì)需要的朋友可以參考下2022-11-11nginx如何通過proxy_pass設(shè)置反向代理,隱藏端口號(hào)
這篇文章主要介紹了nginx如何通過proxy_pass設(shè)置反向代理,隱藏端口號(hào)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01Nginx中IF語句實(shí)現(xiàn)數(shù)學(xué)比較功能
這篇文章主要介紹了Nginx中IF語句實(shí)現(xiàn)數(shù)學(xué)比較功能,即在Nginx中用if判斷數(shù)字大小,類似編程語言中的邏輯比較,需要的朋友可以參考下2015-02-02Nginx HTTPS實(shí)現(xiàn)原理及配置實(shí)踐
本文主要介紹了Nginx HTTPS實(shí)現(xiàn)原理及配置實(shí)踐,詳細(xì)的介紹了HTTPS原理,實(shí)現(xiàn)及其HTTPS單臺(tái)配置實(shí)踐,集群配置、優(yōu)化等,具有一定的參考價(jià)值,感興趣的可以了解一下2023-09-09