ngx_http_upstream_check_module

该模块可以为Nginx提供主动式后端服务器健康检查的功能。

该模块在Nginx-1.4.0版本以前没有默认开启,它可以在配置编译选项的时候开启:./configure --with-http_upstream_check_module

Examples
 
 
  
  1. http { 
  2.     upstream cluster { 
  3.         # simple round-robin 
  4.         server 192.168.0.1:80; 
  5.         server 192.168.0.2:80; 
  6.  
  7.         check interval=3000 rise=2 fall=5 timeout=1000 type=http
  8.         check_http_send "GET / HTTP/1.0\r\n\r\n"; 
  9.         check_http_expect_alive http_2xx http_3xx; 
  10.     } 
  11.  
  12.     server { 
  13.         listen 80; 
  14.  
  15.         location / { 
  16.             proxy_pass http://cluster; 
  17.         } 
  18.  
  19.         location /status { 
  20.             check_status; 
  21.  
  22.             access_log   off; 
  23.             allow SOME.IP.ADD.RESS; 
  24.             deny all; 
  25.        } 
  26.     } 
指令

 

 
  
  1. Syntax: check interval=milliseconds [fall=count] [rise=count] [timeout=milliseconds] [default_down=true|false] [type=tcp|http|ssl_hello|mysql|ajp] [port=check_port
  2. Default: 如果没有配置参数,默认值是:interval=30000 fall=5 rise=2 timeout=1000 default_down=true type=tcp 
  3. 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。

 

Syntax: check_http_send http_packet

Default: "GET / HTTP/1.0\r\n\r\n"

Context: upstream

该指令可以配置http健康检查包发送的请求内容。

 

Syntax: check_http_expect_alive [ http_2xx | http_3xx | http_4xx | http_5xx ]

Default: http_2xx | http_3xx

Context: upstream

该指令指定HTTP回复的成功状态,默认认为2XX和3XX的状态是健康的。

 

Syntax: check_shm_size size

Default: 1M

Context: http

所有的后端服务器健康检查状态都存于共享内存中,该指令可以设置共享内存的大小。默认是1M,如果你有1千台以上的服务器并在配置的时候出现了错误,就可能需要扩大该内存的大小。

 

Syntax: check_status [html|csv|json]

Default: check_status html

Context: location

显示服务器的健康状态页面。该指令需要在http块中配置。

在Tengine-1.4.0以后,你可以配置显示页面的格式。支持的格式有: htmlcsv、 json。默认类型是html

你也可以通过请求的参数来指定格式,假设‘/status’是你状态页面的URL, format参数改变页面的格式,比如:

/status?format=html /status?format=csv /status?format=json 

同时你也可以通过status参数来获取相同服务器状态的列表,比如:

/status?format=html&status=down /status?format=csv&status=up 

下面是一个HTML状态页面的例子(server number是后端服务器的数量,generation是Nginx reload的次数。Index是服务器的索引,Upstream是在配置中upstream的名称,Name是服务器IP,Status是服务器的状态,Rise是服务器连续检查成功的次数,Fall是连续检查失败的次数,Check type是检查的方式,Check port是后端专门为健康检查设置的端口):

 

 
  
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN 
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
  3. <html xmlns="http://www.w3.org/1999/xhtml"> 
  4. <head> 
  5. <title>Nginx http upstream check status</title> 
  6. </head> 
  7. <body> 
  8.     <h1>Nginx http upstream check status</h1> 
  9.     <h2>Check upstream server number: 1, generation: 3</h2> 
  10.     <table style="background-color:white" cellspacing="0"        cellpadding="3" border="1"> 
  11.         <tr bgcolor="#C0C0C0"> 
  12.             <th>Index</th> 
  13.             <th>Upstream</th> 
  14.             <th>Name</th> 
  15.             <th>Status</th> 
  16.             <th>Rise counts</th> 
  17.             <th>Fall counts</th> 
  18.             <th>Check type</th> 
  19.             <th>Check port</th> 
  20.         </tr> 
  21.         <tr> 
  22.             <td>0</td> 
  23.             <td>backend</td> 
  24.             <td>106.187.48.116:80</td> 
  25.             <td>up</td> 
  26.             <td>39</td> 
  27.             <td>0</td> 
  28.             <td>http</td> 
  29.             <td>80</td> 
  30.         </tr> 
  31.     </table> 
  32. </body> 
  33. </html> 

下面是csv格式页面的例子:

0,backend,106.187.48.116:80,up,46,0,http,80 

下面是json格式页面的例子:

 

 
  
  1. {"servers": { 
  2.   "total": 1, 
  3.   "generation": 3, 
  4.   "server": [ 
  5.    {"index": 0, "upstream": "backend", "name": "106.187.48.116:80", "status": "up", "rise": 58, "fall": 0, "type": "http", "port": 80} 
  6.   ] 
  7.  }}