在本小節(jié)我們介紹一個用于Nginx對后端UpStream集群節(jié)點(diǎn)健康狀態(tài)檢查的第三方模塊:nginx_upstream_check_module(https://github.com/yaoweibin/nginx_upstream_check_module)。這個模塊有資料介紹是TaoBao團(tuán)隊(duì)開發(fā)的,但是我在GitHua上試圖求證時并沒有找到直接證據(jù)。
這里需要說明的是,目前有很多Nginx模塊實(shí)現(xiàn)Nginx對后端集群節(jié)點(diǎn)的健康監(jiān)測,不止nginx_upstream_check_module。Nginx官方有一個模塊healthcheck_nginx_upstreams也可以實(shí)現(xiàn)對后端節(jié)點(diǎn)的健康監(jiān)測(https://github.com/cep21/healthcheck_nginx_upstreams有詳細(xì)的安裝和使用介紹)
我們回到對nginx_upstream_check_module的講解,要使用這個第三方模塊首先您需要進(jìn)行下載,然后通過patch命令將補(bǔ)丁打入您原有的Nginx源碼中,并且重新進(jìn)行編譯安裝。下面我們來重點(diǎn)講解一下這個模塊的安裝和使用。
下載nginx_upstream_check_module模塊:
wget https://codeload.github.com/yaoweibin/nginx_upstream_check_module/zip/master
您也可以直接到GitHua上進(jìn)行下載,還一個在linux系統(tǒng)上使用git命令進(jìn)行下載。
解壓安裝,并補(bǔ)丁打入Nginx源碼
# unzip ./nginx_upstream_check_module-master.zip
注意是將補(bǔ)丁打入Nginx源碼,不是Nginx的安裝路徑:
# cd ./nginx-1.6.2
# patch -p1 < ../nginx_upstream_check_module-master/check_1.5.12+.patch
如果補(bǔ)丁安裝成功,您將看到以下的提示信息:
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官網(wǎng)的安裝說明中,有一個打補(bǔ)丁的注意事項(xiàng):
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,那么就應(yīng)該打入check_1.5.12+.patch這個補(bǔ)丁
重新編譯安裝Nginx:
注意重新編譯Nginx,要使用add-module參數(shù)將這個第三方模塊安裝進(jìn)去:
# ./configure –prefix=/usr/nginx-1.6.2/ –add-module=../nginx_upstream_check_module-master/
# make && make install
通過以上的步驟,第三方的nginx_upstream_check_module模塊就在Nginx中準(zhǔn)備好了。接下來我們講解一下如何使用這個模塊。首先看一下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.0rnrn”;
#check_http_expect_alive http_2xx http_3xx;
}
上面的代碼中,check部分就是調(diào)用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:必要參數(shù),檢查請求的間隔時間。
fall:當(dāng)檢查失敗次數(shù)超過了fall,這個服務(wù)節(jié)點(diǎn)就變成down狀態(tài)。
rise:當(dāng)檢查成功的次數(shù)超過了rise,這個服務(wù)節(jié)點(diǎn)又會變成up狀態(tài)。
timeout:請求超時時間,超過等待時間后,這次檢查就算失敗。
default_down:后端服務(wù)器的初始狀態(tài)。默認(rèn)情況下,檢查功能在Nginx啟動的時候?qū)阉泻蠖斯?jié)點(diǎn)的狀態(tài)置為down,檢查成功后,在置為up。
type:這是檢查通信的協(xié)議類型,默認(rèn)為http。以上類型是檢查功能所支持的所有協(xié)議類型。
check_http_send http_packet
http_packet的默認(rèn)格式為:”GET / HTTP/1.0rnrn”
check_http_send設(shè)置,這個設(shè)置描述了檢查模塊在每次檢查時,向后端節(jié)點(diǎn)發(fā)送什么樣的信息
check_http_expect_alive [ http_2xx | http_3xx | http_4xx | http_5xx ]
這些狀態(tài)代碼表示服務(wù)器的HTTP響應(yīng)上是OK的,后端節(jié)點(diǎn)是可用的。默認(rèn)情況的設(shè)置是:http_2xx | http_3xx
當(dāng)您根據(jù)您的配置要求完成檢查模塊的配置后,請首先使用nginx -t 命令監(jiān)測配置文件是否可用,然后在用nginx -s reload重啟nginx。
1.4、不得不提的tengine
Tengine是由淘寶網(wǎng)發(fā)起的Web服務(wù)器項(xiàng)目。它在Nginx的基礎(chǔ)上,針對大訪問量網(wǎng)站的需求,添加了很多高級功能和特性。Tengine的性能和穩(wěn)定性已經(jīng)在大型的網(wǎng)站如淘寶網(wǎng),天貓商城等得到了很好的檢驗(yàn)。它的最終目標(biāo)是打造一個高效、穩(wěn)定、安全、易用的Web平臺(http://tengine.taobao.org/)。
您應(yīng)該懂了,我建議您根據(jù)業(yè)務(wù)的實(shí)際情況,適時在生產(chǎn)環(huán)境引入Tengine。但在本博客發(fā)布時,Tengine的2.X版本還不穩(wěn)定,所以建議實(shí)用1.5.2的穩(wěn)定版本。請記住Tengine就是經(jīng)過升讀改造后的Nginx。