Nginx健康檢查模塊


在本小節我們介紹一個用於Nginx對后端UpStream集群節點健康狀態檢查的第三方模塊:nginx_upstream_check_module(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模塊:

wget https://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。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM