nginx 主动式后端服务器健康检查

简介:

原文链接  http://tengine.taobao.org/document_cn/http_upstream_check_cn.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
ngx_http_upstream_check_module
该模块可以为Tengine提供主动式后端服务器健康检查的功能。
该模块在Tengine - 1.4 . 0 版本以前没有默认开启,它可以在配置编译选项的时候开启:. / configure  - - with - http_upstream_check_module
Examples
http {
     upstream cluster1 {
         # simple round-robin
         server  192.168 . 0.1 : 80 ;
         server  192.168 . 0.2 : 80 ;
 
         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;
     }
 
     upstream cluster2 {
         # simple round-robin
         server  192.168 . 0.3 : 80 ;
         server  192.168 . 0.4 : 80 ;
 
         check interval = 3000  rise = 2  fall = 5  timeout = 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_2xx http_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 ;
         }
     }
}
指令
 
 
Syntax: check interval = 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 = 5  rise = 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
 
 
Syntax: check_keepalive_requests request_num
Default:  1
Context: upstream
该指令可以配置一个连接发送的请求数,其默认值为 1 ,表示Tengine完成 1 次请求后即关闭连接。
 
 
Syntax: check_http_send http_packet
Default:  "GET / HTTP/1.0\r\n\r\n"
Context: upstream
该指令可以配置http健康检查包发送的请求内容。为了减少传输数据量,推荐采用 "HEAD" 方法。
当采用长连接进行健康检查时,需在该指令中添加keep - alive请求头,如: "HEAD / HTTP/1.1\r\nConnection: keep-alive\r\n\r\n" 。 同时,在采用 "GET" 方法的情况下,请求uri的size不宜过大,确保可以在 1 个interval内传输完成,否则会被健康检查模块视为后端服务器或网络异常。
 
 
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 以后,你可以配置显示页面的格式。支持的格式有: html、csv、 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是后端专门为健康检查设置的端口):
<!DOCTYPE html PUBLIC " - / / W3C / / DTD XHTML  1.0  Strict / / EN
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" >
<html xmlns = "http://www.w3.org/1999/xhtml" >
<head>
<title>Nginx http upstream check status< / title>
< / head>
<body>
     <h1>Nginx http upstream check status< / h1>
     <h2>Check upstream server number:  1 , generation:  3 < / h2>
     <table style = "background-color:white"  cellspacing = "0"         cellpadding = "3"  border = "1" >
         <tr bgcolor = "#C0C0C0" >
             <th>Index< / th>
             <th>Upstream< / th>
             <th>Name< / th>
             <th>Status< / th>
             <th>Rise counts< / th>
             <th>Fall counts< / th>
             <th>Check  type < / th>
             <th>Check port< / th>
         < / tr>
         <tr>
             <td> 0 < / td>
             <td>backend< / td>
             <td> 106.187 . 48.116 : 80 < / td>
             <td>up< / td>
             <td> 39 < / td>
             <td> 0 < / td>
             <td>http< / td>
             <td> 80 < / td>
         < / tr>
     < / table>
< / body>
< / html>
下面是csv格式页面的例子:
0 ,backend, 106.187 . 48.116 : 80 ,up, 46 , 0 ,http, 80
下面是json格式页面的例子:
{ "servers" : {
   "total" 1 ,
   "generation" 3 ,
   "server" : [
    { "index" 0 "upstream" "backend" "name" "106.187.48.116:80" "status" "up" "rise" 58 "fall" 0 "type" "http" "port" 80 }
   ]
  }}


查看:http://www.cnblogs.com/rainy-shurun/p/5416160.html



本文转自 Tenderrain 51CTO博客,原文链接:http://blog.51cto.com/tenderrain/1949257

相关文章
|
3月前
|
存储 应用服务中间件 数据库
C++文件服务器项目—Nginx+FastDFS插件—5
C++文件服务器项目—Nginx+FastDFS插件—5
37 0
|
25天前
|
前端开发 应用服务中间件 nginx
使用Docker快速搭建Web服务器Nginx
本文指导如何使用Docker快速搭建Nginx服务器。首先,通过`docker pull`命令获取Nginx镜像,然后以容器形式运行Nginx并映射端口。通过挂载目录实现本地文件与容器共享,便于自定义网页。使用`docker ps`检查运行状态,访问IP:8088确认部署成功。最后,介绍了停止、删除Nginx容器的命令,强调Docker简化了服务器部署和管理。
39 0
|
2天前
|
应用服务中间件 Linux 开发工具
如何在阿里云服务器快速搭建部署Nginx环境
以下是内容的摘要: 本文档主要介绍了在阿里云上购买和配置服务器的步骤,包括注册阿里云账号、实名认证、选择和购买云服务器、配置安全组、使用Xshell和Xftp进行远程连接和文件传输,以及安装和配置Nginx服务器的过程。在完成这些步骤后,你将能够在服务器上部署和运行自己的网站或应用。
|
7天前
|
弹性计算 应用服务中间件 Linux
阿里云ECS服务器上从零开始搭建nginx服务器
阿里云ECS服务器上从零开始搭建nginx服务器
|
2月前
|
网络协议 Unix 应用服务中间件
如何进行 Nginx HTTPS服务器搭建
【2月更文挑战第6天】
63 0
|
2月前
|
负载均衡 JavaScript 应用服务中间件
强大的WEB服务器-Nginx
强大的WEB服务器-Nginx
29 0
|
2月前
|
Kubernetes 应用服务中间件 nginx
K8S Pod Sidecar 应用场景之一 - 加入 NGINX Sidecar 做反代和 web 服务器
K8S Pod Sidecar 应用场景之一 - 加入 NGINX Sidecar 做反代和 web 服务器
|
2月前
|
应用服务中间件 nginx
将树莓派转身为强大的Web服务器:使用Nginx和cpolar实现远程访问
安装 Nginx(发音为“engine-x”)可以将您的树莓派变成一个强大的 Web 服务器,可以用于托管网站或 Web 应用程序。相比其他 Web 服务器,Nginx 的内存占用率非常低,可以在树莓派等资源受限的设备上运行。同时结合cpolar 内网穿透工具即可实现远程访问。
|
3月前
|
负载均衡 Ubuntu 应用服务中间件
如何在Nginx上阻止特定IP地址的访问,以增强服务器的安全性
如何在Nginx上阻止特定IP地址的访问,以增强服务器的安全性
38 0
如何在Nginx上阻止特定IP地址的访问,以增强服务器的安全性
|
3月前
|
TensorFlow 算法框架/工具 数据安全/隐私保护
如何在云服务器使用docker快速部署jupyter web服务器(Nginx+docker+jupyter+tensorflow)
如何在云服务器使用docker快速部署jupyter web服务器(Nginx+docker+jupyter+tensorflow)
77 0