Nginx健康檢查模組
在本小節我們介紹一個用於Nginx對後端UpStream叢集節點健康狀態檢查的第三方模組:nginx_upstream_check_module(ofollow,noindex" target="_blank">https://github.com/yaoweibin/nginx_upstream_check_module )。這個模組有資料介紹是TaoBao團隊開發的,但是我在GitHua上試圖求證時並沒有找到直接證據。
這裡需要說明的是,目前有很多Nginx模組實現Nginx對後端叢集節點的健康監測,不止nginx_upstream_check_module。Nginx官方有一個模組healthcheck_nginx_upstreams也可以實現對後端節點的健康監測(https://github.com/cep21/healthcheck_nginx_upstreams 有詳細的安裝和使用介紹)
我們回到對nginx_upstream_check_module的講解,要使用這個第三方模組首先您需要進行下載,然後通過patch命令將補丁打入您原有的Nginx原始碼中,並且重新進行編譯安裝。下面我們來重點講解一下這個模組的安裝和使用。
下載nginx_upstream_check_module模組:
wgethttps://codeload.github.com/yaoweibin/nginx_upstream_check_module/zip/master
您也可以直接到GitHua上進行下載,還一個在linux系統上使用git命令進行下載。
解壓安裝,並補丁打入Nginx原始碼
# unzip ./nginx_upstream_check_module-master.zip
注意是將補丁打入Nginx原始碼,不是Nginx的安裝路徑:
# cd ./nginx-1.6.2
# patch -p1 < ../nginx_upstream_check_module-master/check_1.5.12+.patch
如果補丁安裝成功,您將看到以下的提示資訊:
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_upstream_check_module官網的安裝說明中,有一個打補丁的注意事項:
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'.
這裡我們的Nginx的版本是1.6.2,那麼就應該打入check_1.5.12+.patch這個補丁
重新編譯安裝Nginx:
注意重新編譯Nginx,要使用add-module引數將這個第三方模組安裝進去:
# ./configure --prefix=/usr/nginx-1.6.2/ --add-module=../nginx_upstream_check_module-master/
# make && make install
通過以上的步驟,第三方的nginx_upstream_check_module模組就在Nginx中準備好了。接下來我們講解一下如何使用這個模組。首先看一下upstream的配置資訊:
upstream cluster {
# simple round-robin
server 192.168.0.1:80;
server 192.168.0.2: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。以上型別是檢查功能所支援的所有協議型別。
check_http_send http_packet
http_packet的預設格式為:"GET / HTTP/1.0\r\n\r\n"
check_http_send設定,這個設定描述了檢查模組在每次檢查時,向後端節點發送什麼樣的資訊
check_http_expect_alive [ http_2xx | http_3xx | http_4xx | http_5xx ]
這些狀態程式碼表示伺服器的HTTP響應上是OK的,後端節點是可用的。預設情況的設定是:http_2xx | http_3xx
當您根據您的配置要求完成檢查模組的配置後,請首先使用nginx -t 命令監測配置檔案是否可用,然後在用nginx -s reload重啟nginx。
1.4、不得不提的tengine
Tengine是由淘寶網發起的Web伺服器專案。它在Nginx的基礎上,針對大訪問量網站的需求,添加了很多高階功能和特性。Tengine的效能和穩定性已經在大型的網站如淘寶網,天貓商城等得到了很好的檢驗。它的最終目標是打造一個高效、穩定、安全、易用的Web平臺(http://tengine.taobao.org/ )。
您應該懂了,我建議您根據業務的實際情況,適時在生產環境引入Tengine。但在本部落格釋出時,Tengine的2.X版本還不穩定,所以建議實用1.5.2的穩定版本。請記住Tengine就是經過升讀改造後的Nginx。
Linux公社的RSS地址 :https://www.linuxidc.com/rssFeed.aspx
本文永久更新連結地址:https://www.linuxidc.com/Linux/2018-10/154667.htm