Travis CI 建置詳細資訊:點擊查看
(中文版本請參看這裡)
Nginx 上游伺服器的健康檢查器(支援 http 上游 && 流上游)
此模組可為NGINX提供主動後端伺服器健康檢查的能力(支援4台和7台後端伺服器的健康檢查)。
這個nginx模組還在開發中,你可以幫忙改進它。
專案在開發中也進展順利,歡迎您貢獻程式碼,或回報bug。共同努力,讓事情變得更好。
如果您有任何疑問,請與我聯絡:
QQ
:373882405mail
:[email protected]當你使用nginx作為負載平衡器時,nginx原生只提供基本的重試來保證存取正常的後端伺服器。
相較之下,這個nginx第三方模組為後端伺服器提供主動健康狀態偵測。
它維護一個後端伺服器列表,保證新請求直接發送到健康的後端伺服器。
主要特點:
tcp
/ udp
/ http
http
/ fastcgi
html
/ json
/ csv
/ prometheus
html
/ json
/ csv
/ prometheus
check_http_expect_body ~ ".+OK.+";
git clone https://github.com/nginx/nginx.git
git clone https://github.com/zhouchangxun/ngx_healthcheck_module.git
cd nginx/;
git checkout branches/stable-1.12
git apply ../ngx_healthcheck_module/nginx_healthcheck_for_nginx_1.12+.patch
./auto/configure --with-stream --add-module=../ngx_healthcheck_module/
make && make install
回目錄
nginx.conf 範例
user root;
worker_processes 1 ;
error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024 ;
}
http {
server {
listen 80 ;
# status interface
location /status {
healthcheck_status json;
}
# http front
location / {
proxy_pass http://http-cluster;
}
}
# as a backend server.
server {
listen 8080 ;
location / {
root html;
}
}
upstream http-cluster {
# simple round-robin
server 127.0.0.1:8080;
server 127.0.0.2:81;
check interval=3000 rise=2 fall=5 timeout=5000 type=http;
check_http_send "GET / HTTP/1.0rnrn" ;
check_http_expect_alive http_2xx http_3xx;
}
}
stream {
upstream tcp-cluster {
# simple round-robin
server 127.0.0.1:22;
server 192.168.0.2:22;
check interval=3000 rise=2 fall=5 timeout=5000 default_down=true type=tcp;
}
server {
listen 522 ;
proxy_pass tcp-cluster;
}
upstream udp-cluster {
# simple round-robin
server 127.0.0.1:53;
server 8.8.8.8:53;
check interval=3000 rise=2 fall=5 timeout=5000 default_down=true type=udp;
}
server {
listen 53 udp;
proxy_pass udp-cluster;
}
}
狀態介面
一個典型的輸出是(json格式)
root @ changxun - PC : ~ / nginx - dev / ngx_healthcheck_module # curl localhost/status
{ "servers" : {
"total" : 6 ,
"generation" : 3 ,
"http" : [
{ "index" : 0 , "upstream" : "http-cluster" , "name" : "127.0.0.1:8080" , "status" : "up" , "rise" : 119 , "fall" : 0 , "type" : "http" , "port" : 0 },
{ "index" : 1 , "upstream" : "http-cluster" , "name" : "127.0.0.2:81" , "status" : "down" , "rise" : 0 , "fall" : 120 , "type" : "http" , "port" : 0 }
],
"stream" : [
{ "index" : 0 , "upstream" : "tcp-cluster" , "name" : "127.0.0.1:22" , "status" : "up" , "rise" : 22 , "fall" : 0 , "type" : "tcp" , "port" : 0 },
{ "index" : 1 , "upstream" : "tcp-cluster" , "name" : "192.168.0.2:22" , "status" : "down" , "rise" : 0 , "fall" : 7 , "type" : "tcp" , "port" : 0 },
{ "index" : 2 , "upstream" : "udp-cluster" , "name" : "127.0.0.1:53" , "status" : "down" , "rise" : 0 , "fall" : 120 , "type" : "udp" , "port" : 0 },
{ "index" : 3 , "upstream" : "udp-cluster" , "name" : "8.8.8.8:53" , "status" : "up" , "rise" : 3 , "fall" : 0 , "type" : "udp" , "port" : 0 }
]
}}
root @ changxun - PC : ~ / nginx - dev / ngx_healthcheck_module #
或(普羅米修斯格式)
root @ changxun - PC : ~ / nginx - dev / ngx_healthcheck_module # curl localhost/status
# HELP nginx_upstream_count_total Nginx total number of servers
# TYPE nginx_upstream_count_total gauge
nginx_upstream_count_total 6
# HELP nginx_upstream_count_up Nginx total number of servers that are UP
# TYPE nginx_upstream_count_up gauge
nginx_upstream_count_up 0
# HELP nginx_upstream_count_down Nginx total number of servers that are DOWN
# TYPE nginx_upstream_count_down gauge
nginx_upstream_count_down 6
# HELP nginx_upstream_count_generation Nginx generation
# TYPE nginx_upstream_count_generation gauge
nginx_upstream_count_generation 1
# HELP nginx_upstream_server_rise Nginx rise counter
# TYPE nginx_upstream_server_rise counter
nginx_upstream_server_rise { index = "0" , upstream_type = "http" , upstream = "http-cluster" , name = "127.0.0.1:8082" , status = "down" , type = "http" , port = "0" } 0
nginx_upstream_server_rise { index = "1" , upstream_type = "http" , upstream = "http-cluster" , name = "127.0.0.2:8082" , status = "down" , type = "http" , port = "0" } 0
nginx_upstream_server_rise { index = "1" , upstream_type = "stream" , upstream = "tcp-cluster" , name = "192.168.0.2:22" , status = "down" , type = "tcp" , port = "0" } 0
nginx_upstream_server_rise { index = "2" , upstream_type = "stream" , upstream = "udp-cluster" , name = "127.0.0.1:5432" , status = "down" , type = "udp" , port = "0" } 0
nginx_upstream_server_rise { index = "4" , upstream_type = "stream" , upstream = "http-cluster2" , name = "127.0.0.1:8082" , status = "down" , type = "http" , port = "0" } 0
nginx_upstream_server_rise { index = "5" , upstream_type = "stream" , upstream = "http-cluster2" , name = "127.0.0.2:8082" , status = "down" , type = "http" , port = "0" } 0
# HELP nginx_upstream_server_fall Nginx fall counter
# TYPE nginx_upstream_server_fall counter
nginx_upstream_server_fall { index = "0" , upstream_type = "http" , upstream = "http-cluster" , name = "127.0.0.1:8082" , status = "down" , type = "http" , port = "0" } 41
nginx_upstream_server_fall { index = "1" , upstream_type = "http" , upstream = "http-cluster" , name = "127.0.0.2:8082" , status = "down" , type = "http" , port = "0" } 42
nginx_upstream_server_fall { index = "1" , upstream_type = "stream" , upstream = "tcp-cluster" , name = "192.168.0.2:22" , status = "down" , type = "tcp" , port = "0" } 14
nginx_upstream_server_fall { index = "2" , upstream_type = "stream" , upstream = "udp-cluster" , name = "127.0.0.1:5432" , status = "down" , type = "udp" , port = "0" } 40
nginx_upstream_server_fall { index = "4" , upstream_type = "stream" , upstream = "http-cluster2" , name = "127.0.0.1:8082" , status = "down" , type = "http" , port = "0" } 40
nginx_upstream_server_fall { index = "5" , upstream_type = "stream" , upstream = "http-cluster2" , name = "127.0.0.2:8082" , status = "down" , type = "http" , port = "0" } 43
# HELP nginx_upstream_server_active Nginx active 1 for UP / 0 for DOWN
# TYPE nginx_upstream_server_active gauge
nginx_upstream_server_active { index = "0" , upstream_type = "http" , upstream = "http-cluster" , name = "127.0.0.1:8082" , type = "http" , port = "0" } 0
nginx_upstream_server_active { index = "1" , upstream_type = "http" , upstream = "http-cluster" , name = "127.0.0.2:8082" , type = "http" , port = "0" } 0
nginx_upstream_server_active { index = "1" , upstream_type = "stream" , upstream = "tcp-cluster" , name = "192.168.0.2:22" , type = "tcp" , port = "0" } 0
nginx_upstream_server_active { index = "2" , upstream_type = "stream" , upstream = "udp-cluster" , name = "127.0.0.1:5432" , type = "udp" , port = "0" } 0
nginx_upstream_server_active { index = "4" , upstream_type = "stream" , upstream = "http-cluster2" , name = "127.0.0.1:8082" , type = "http" , port = "0" } 0
nginx_upstream_server_active { index = "5" , upstream_type = "stream" , upstream = "http-cluster2" , name = "127.0.0.2:8082" , type = "http" , port = "0" } 0
root @ changxun - PC : ~ / nginx - dev / ngx_healthcheck_module #
回目錄
Syntax
檢查間隔=毫秒 [fall=count] [rise=count] [逾時=毫秒] [default_down=true|false] [type=tcp|udp|http] [port=check_port]
Default
:間隔=30000 下降=5 上升=2 逾時=1000 default_down=true 類型=tcp
Context
:http/upstream ||河/上游
該命令可以開啟後端伺服器健康檢查功能。
Detail
interval
:傳送健康檢查包給後端的時間間隔。fall
( fall_count
):如果連續失敗次數達到fall_count,則認為伺服器宕機。rise
( rise_count
):若連續成功次數達到rise_count,則認為伺服器已上線。timeout
:後端健康請求逾時。default_down
:設定伺服器的初始狀態,如果為true,則表示預設為down,如果為false,則為up。預設值為true,即一開始伺服器不可用,要等待健康檢查包達到一定次數後才認為健康。type
:健康檢查包類型,目前支援以下類型tcp
:簡單的tcp連接,如果連接成功,則表示後端正常。udp
:簡單發送udp封包,如果收到icmp錯誤(主機或連接埠不可達),則顯示後端異常。http
:發送HTTP請求,透過後端回覆包的狀態來判斷後端是否存活。範例如下:
stream {
upstream tcp-cluster {
# simple round-robin
server 127.0.0.1:22;
server 192.168.0.2:22;
check interval=3000 rise=2 fall=5 timeout=5000 default_down=true type=tcp;
}
server {
listen 522 ;
proxy_pass tcp-cluster;
}
...
}
Syntax
:healthcheck_status [html|csv|json|prometheus]
Default
:healthcheck_status html
Context
:http/伺服器/位置
範例如下:
http {
server {
listen 80 ;
# status interface
location /status {
healthcheck_status;
}
...
}
您可以指定預設顯示格式。格式可以是html
、 csv
或json
。預設類型是html
。它還支援透過請求參數指定格式。假設你的check_status
位置是'/status', format
參數可以改變顯示頁面的格式。你可以這樣做:
/狀態?
/狀態?
/狀態?
/狀態?
目前,您可以透過status
參數取得具有相同狀態的伺服器清單。例如:
/status?format=json&status=down
/status?format=html&status=down
/status?format=csv&status=up
/status?format=prometheus&status=up
回目錄
回目錄
請回報錯誤
或透過以下方式提交補丁
回目錄
Chance Chou (週長勳) [email protected]。
回目錄
健康檢查部分基於姚維斌的健康檢查模組nginx_upstream_check_module(http://github.com/yaoweibin/nginx_upstream_check_module);
該模組根據 BSD 許可證獲得許可。
版權所有(C)2017-, by 週昌勳 [email protected]
版權所有(C) 2014 姚偉斌 [email protected]
版權所有。
如果滿足以下條件,則允許以原始程式碼和二進位形式重新分發和使用,無論是否經過修改:
原始碼的重新散佈必須保留上述版權聲明、此條件清單和以下免責聲明。
以二進位形式重新散佈必須在隨散佈提供的文件和/或其他資料中複製上述版權聲明、此條件清單以及以下免責聲明。
本軟體由版權所有者和貢獻者「按原樣」提供,不承擔任何明示或默示的保證,包括但不限於適銷性和特定用途適用性的默示保證。在任何情況下,版權持有者或貢獻者均不對任何直接、間接、附帶、特殊、懲戒性或後果性損害(包括但不限於採購替代商品或服務;使用、數據或利潤損失;或其他損失)承擔責任。被告知可能發生此類損害。
回目錄
回目錄