Nginx負載均衡監測節點狀態
v插件(ngx_http_upstream_check_module)
upstream_check_module介紹:
該模塊可以爲Tengine提供主動式後端服務器健康檢查的功能。
該模塊在Tengine-1.4.0版本以前沒有默認開啓,它可以在配置編譯選項的時候開啓:./configure--with-http_upstream_check_module
upstream_check_module官方文檔
http://tengine.taobao.org/document_cn/http_upstream_check_cn.html
upstream_check_module下載地址
https://github.com/yaoweibin/nginx_upstream_check_module
給nginx打上補丁的安裝
unzip nginx_upstream_check_module-master.zip useradd nginx -s /sbin/nologin -M tar xf nginx-1.9.2.tar.gz cd nginx-1.9.2 patch -p0 </root/nginx_upstream_check_module-master/check_1.9.2+.patch ./configure \ --prefix=/application/nginx-1.9.2 \ --user=nginx \ --group=nginx \ --with-http_ssl_module \ --with-http_stub_status_module \ --add-module=/root/nginx_upstream_check_module-master make make install ln -s /application/nginx-1.9.2/ /application/nginx
patch參數說明
-p0 選項要從當前目錄查找目的文件(夾)
-p1 選項要忽略掉第一層目錄,從當前目錄開始查找。
在這裏以實例說明:
old/modules/pcitable
如果使用參數-p0,那就表示從當前目錄找一個叫做old的文件夾,在它下面尋找modules下的pcitable文件來執行patch操作。
如果使用參數-p1,那就表示忽略第一層目錄(即不管old),從當前目錄尋找modules的文件夾,在它下面找pcitable。這樣的前提是當前目錄必須爲modules所在的目錄。
upstream_check_module語法
Syntax: checkinterval=milliseconds [fall=count] [rise=count] [timeout=milliseconds][default_down=true|false] [type=tcp|http|ssl_hello|mysql|ajp] [port=check_port]
Default: 如果沒有配置參數,默認值是:interval=30000 fall=5rise=2 timeout=1000 default_down=true type=tcp
Context: upstream
指令後面的參數意義是:
interval:向後端發送的健康檢查包的間隔。單位是毫秒。
fall(fall_count): 如果連續失敗次數達到fall_count,服務器就被認爲是down。
rise(rise_count): 如果連續成功次數達到rise_count,服務器就被認爲是up。
timeout: 後端健康請求的超時時間。單位是毫秒。
default_down: 設定初始時服務器的狀態,如果是true,就說明默認是down的,如果是false,就是up的。默認值是true,也就是一開始服務器認爲是不可用,要等健康檢查包達到一定成功次數以後纔會被認爲是健康的。
type:健康檢查包的類型,現在支持以下多種類型
tcp:簡單的tcp連接,如果連接成功,就說明後端正常。
ssl_hello:發送一個初始的SSL hello包並接受服務器的SSL hello包。
http:發送HTTP請求,通過後端的回覆包的狀態來判斷後端是否存活。
mysql: 向mysql服務器連接,通過接收服務器的greeting包來判斷後端是否存活。
ajp:向後端發送AJP協議的Cping包,通過接收Cpong包來判斷後端是否存活。
port: 指定後端服務器的檢查端口。你可以指定不同於真實服務的後端服務器的端口,比如後端提供的是443端口的應用,你可以去檢查80端口的狀態來判斷後端健康狀況。默認是0,表示跟後端server提供真實服務的端口一樣。該選項出現於Tengine-1.4.0。
例:
http { upstream cluster1 { # simple round-robin server 192.168.0.1:80; server 192.168.0.2:80; check interval=3000 rise=2 fall=5timeout=1000 type=http; check_http_send "HEAD /HTTP/1.0\r\n\r\n"; check_http_expect_alive http_2xxhttp_3xx; } upstream cluster2 { # simple round-robin server 192.168.0.3:80; server 192.168.0.4:80; check interval=3000 rise=2 fall=5timeout=1000 type=http; check_keepalive_requests 100; check_http_send "HEAD /HTTP/1.1\r\nConnection: keep-alive\r\n\r\n"; check_http_expect_alive http_2xxhttp_3xx; } server { listen 80; location /1 { proxy_pass http://cluster1; } location /2 { proxy_pass http://cluster2; } location /status { check_status; access_log off; allow SOME.IP.ADD.RESS; deny all; } } }
配置
http { upstream dynamic_pools { server192.168.10.30; server192.168.10.31; check interval=3000 rise=2 fall=5timeout=1000; # interval檢測間隔時間,單位爲毫秒,rsie請求2次正常的話,標記此realserver的狀態爲up,fall表示請求5次都失敗的情況下,標記此realserver的狀態爲down,timeout爲超時時間,單位爲毫秒。 } server { listen 80; server_name www.etiantian.org; location / { proxy_pass http://dynamic_pools; includeproxy.conf; } location /nstatus { check_status; access_log off; #不記錄訪問日誌 allow192.168.10.0/24; #允許的ip地址(段) deny all; #除過允許的ip地址(段)拒絕所有ip訪問 } } }
瀏覽器訪問