一、环境需求
服务 |
数量 |
用途 |
nginx (建议使用 1.20 以上版本,此文档使用 1.25) |
1 |
配置健康检测,验证配置是否生效 |
python (建议使用 3.6 以上版本,此文档使用 3.9) |
进程*3 |
模拟后端服务 |
二、健康检查配置
主动
原理及说明
主动健康检测是指 Nginx 主动向后端服务器发送特定的请求,并根据服务器的响应来判断其健康状态。Nginx 会按照预设的时间间隔,向后端服务器发送健康检查请求(通常是 HTTP 请求),然后根据响应的状态码、响应时间等信息来决定后端服务器是否可用
使用模块 nginx_upstream_check_module,需要部署编译
建议配合监控服务使用,因为就算可以自动切换流量,也无法自动修复故障节点,需要手动恢复
配置方法
http {
upstream backend {
server backend1.example.com:8080;
server backend2.example.com:8080;
check interval=5s rise=2 fall=3 timeout=1000 type=http;
check_http_send "HEAD /health HTTP/1.0\r\n\r\n";
check_http_expect_alive http_2xx http_3xx;
}
server {
location /status {
check_status;
access_log off;
}
}
}
interval=5s:表示每隔 5 秒对后端服务器进行一次健康检查。
rise=2:如果后端服务器连续 2 次健康检查成功,则认为该服务器恢复可用状态。
fall=3:如果后端服务器连续 3 次健康检查失败,则认为该服务器不可用。
timeout=1000:健康检查请求的超时时间为 1000 毫秒(即 1 秒),如果在这个时间内没有收到响应,则认为检查失败。
type=http:指定健康检查的类型为 HTTP。
check_http_send "HEAD /health HTTP/1.0\r\n\r\n";:Nginx 在进行健康检查时,会向后端服务器发送一个 HTTP HEAD 请求,请求的路径为 /health。使用 HEAD 请求可以只获取响应头,而不获取响应体,从而减少网络开销,如有特殊需求也可以使用GET
check_http_expect_alive http_2xx http_3xx;:表示如果后端服务器返回的状态码是 2xx 或 3xx,则认为健康检查成功。
location /status : 这个资源路径是check模块自带的健康状态页面,例如

实际用途
- 及时发现故障:可以在后端服务器出现问题的早期就检测到,避免将请求转发到不可用的服务器上,提高服务的可用性。
- 动态调整负载均衡:当检测到某台后端服务器不可用时,Nginx 会自动将其从可用服务器列表中移除,将请求转发到其他健康的服务器;当服务器恢复正常后,再将其重新加入可用列表。
- 监控服务器性能:通过分析健康检查的响应时间等指标,可以监控后端服务器的性能变化,及时发现潜在的性能问题。
被动
原理及说明
被动健康检测是指 Nginx 在处理客户端请求时,根据后端服务器的响应结果来判断其健康状态。当请求出现错误(如返回 500、502、503、504 错误或者超时等)时,Nginx 会认为该后端服务器可能出现了问题,并根据配置进行相应的处理,如将请求转发到其他后端服务器
一般来说 nginx 都会自带此功能无需另外编译模块
同样建议配合监控服务使用
配置方法
http {
upstream backend {
server backend1.example.com max_fails=3 fail_timeout=10s;
server backend2.example.com max_fails=3 fail_timeout=10s;
server backend2.example.com backup
}
server {
proxy_next_upstream_tries 2;
proxy_next_upstream_timeout 5s;
}
}
max_fails=3:在 fail_timeout 时间内,该后端服务器允许的连续失败次数,达到 3 次则会被标记为不可用。
fail_timeout=10s:一是检测失败的时间窗口为 10 秒;二是服务器被标记为不可用的时长为 10 秒,期间 Nginx 不会向其转发请求。
backup:backup 表示该服务器作为备用服务器,只有当所有主服务器(未标记为 backup 的服务器)都不可用时,才会参与负载均衡处理请求。
proxy_next_upstream_tries 2:当请求转发到后端服务器出现失败时,Nginx 最多尝试将请求转发到下一个可用的后端服务器 2 次(加上初始的一次请求,总共最多尝试 3 次)。
proxy_next_upstream_timeout 5s:每次尝试将请求转发到下一个后端服务器的超时时间为 5 秒,若在 5 秒内未得到响应,则认为此次转发失败。
实际用途
- 故障转移:当后端服务器出现临时故障时,Nginx 可以快速将请求转发到其他可用的服务器,保证服务的连续性。
- 减轻故障服务器负担:当某台后端服务器出现问题时,减少对其的请求,避免其进一步过载,有助于服务器恢复正常。
- 提高服务稳定性:通过自动处理后端服务器的错误响应,减少因服务器故障对客户端造成的影响,提高整个服务的稳定性。
三、效果测试
nginx 配置
upstream test {
server 10.10.11.134:20001 max_fails=3 fail_timeout=30s;
server 10.10.11.134:20002 max_fails=3 fail_timeout=30s;
server 10.10.11.134:20003 backup;
}
upstream test1 {
server 10.10.11.134:20001;
server 10.10.11.134:20002;
server 10.10.11.134:20003;
check interval=5000 rise=2 fall=5 timeout=1000 type=http;
check_http_send "GET / HTTP/1.0\r\n\r\n";
check_http_expect_alive http_2xx http_3xx;
}
server{
listen 80;
charset utf-8;
server_name 10.10.11.134;
client_max_body_size 500m;
proxy_read_timeout 3600000;
proxy_next_upstream_tries 2;
proxy_next_upstream_timeout 5s;
location /test/ {
proxy_pass http://test/;
}
location /test1/ {
proxy_pass http://test1/;
}
location /status {
# check_status是第三方插件自带的功能,用于展示服务列表的健康检查结果界面
check_status;
access_log off;
}
}
后端程序(Python)
import http.server
import socketserver
PORT = 20001
RESPONSE_STRING = "Python test 1"
class MyHandler(http.server.SimpleHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header('Content-type', 'text/plain')
self.end_headers()
self.wfile.write(RESPONSE_STRING.encode())
def run(server_class=socketserver.TCPServer, handler_class=MyHandler, port=PORT):
server_address = ('', port)
httpd = server_class(server_address, handler_class)
print(f"Starting server on port {port}")
try:
httpd.serve_forever()
except KeyboardInterrupt:
pass
httpd.server_close()
print("Stopping server...")
if __name__ == "__main__":
run()
程序启动测试
- 三个 python 程序依次打开三个终端窗口进行测试

[root@minio3 nginx]
Python test 1[root@minio3 nginx]
Python test 2[root@minio3 nginx]
Python test 3[root@minio3 nginx]
Python test 1[root@minio3 nginx]
Python test 2[root@minio3 nginx]
Python test 3[root@minio3 nginx]
Python test 1[root@minio3 nginx]
Python test 1[root@minio3 nginx]
Python test 2[root@minio3 nginx]
Python test 3[root@minio3 nginx]
[root@minio3 nginx]
Python test 2[root@minio3 nginx]
Python test 1[root@minio3 nginx]
Python test 2[root@minio3 nginx]
Python test 1[root@minio3 nginx]
Python test 2[root@minio3 nginx]
Python test 2[root@minio3 nginx]
[root@minio3 nginx]
Python test 2[root@minio3 nginx]
Python test 3[root@minio3 nginx]
Python test 2[root@minio3 nginx]
Python test 3[root@minio3 nginx]
Python test 2[root@minio3 nginx]
Python test 3[root@minio3 nginx]
[root@minio3 nginx]
Python test 2[root@minio3 nginx]
Python test 2[root@minio3 nginx]
Python test 2[root@minio3 nginx]
Python test 2[root@minio3 nginx]
Python test 2[root@minio3 nginx]
[root@minio3 nginx]
Python test 3[root@minio3 nginx]
Python test 3[root@minio3 nginx]
Python test 3[root@minio3 nginx]
Python test 3[root@minio3 nginx]
Python test 3[root@minio3 nginx]
[root@minio3 nginx]
<html>
<head><title>502 Bad Gateway</title></head>
<body>
<center><h1>502 Bad Gateway</h1></center>
<hr><center>nginx</center>
</body>
</html>
[root@minio3 nginx]
<html>
<head><title>502 Bad Gateway</title></head>
<body>
<center><h1>502 Bad Gateway</h1></center>
<hr><center>nginx</center>
</body>
</html>
[root@minio3 nginx]
Python test 3[root@minio3 nginx]
Python test 3[root@minio3 nginx]
Python test 3[root@minio3 nginx]
Python test 3[root@minio3 nginx]
Python test 3[root@minio3 nginx]
Python test 3[root@minio3 nginx]
[root@minio3 nginx]
<html>
<head><title>502 Bad Gateway</title></head>
<body>
<center><h1>502 Bad Gateway</h1></center>
<hr><center>nginx</center>
</body>
</html>
[root@minio3 nginx]
<html>
<head><title>502 Bad Gateway</title></head>
<body>
<center><h1>502 Bad Gateway</h1></center>
<hr><center>nginx</center>
</body>
</html>
[root@minio3 nginx]
<html>
<head><title>502 Bad Gateway</title></head>
<body>
<center><h1>502 Bad Gateway</h1></center>
<hr><center>nginx</center>
</body>
</html>
[root@minio3 nginx]
Python test 1[root@minio3 nginx]
Python test 1[root@minio3 nginx]
Python test 1[root@minio3 nginx]
Python test 1[root@minio3 nginx]
[root@minio3 nginx]
Python test 1[root@minio3 nginx]
Python test 1[root@minio3 nginx]
Python test 1[root@minio3 nginx]
Python test 1[root@minio3 nginx]
Python test 1[root@minio3 nginx]
Python test 1[root@minio3 nginx]
[root@minio3 nginx]
Python test 1[root@minio3 nginx]
Python test 1[root@minio3 nginx]
Python test 1[root@minio3 nginx]
Python test 1[root@minio3 nginx]
Python test 1[root@minio3 nginx]
Python test 1[root@minio3 nginx]
Python test 1[root@minio3 nginx]
Python test 1[root@minio3 nginx]
Python test 1[root@minio3 nginx]
Python test 1[root@minio3 nginx]
Python test 1[root@minio3 nginx]
Python test 2[root@minio3 nginx]
Python test 1[root@minio3 nginx]
Python test 2[root@minio3 nginx]
Python test 1[root@minio3 nginx]
[root@minio3 nginx]
Python test 2[root@minio3 nginx]
Python test 1[root@minio3 nginx]
Python test 2[root@minio3 nginx]
Python test 1[root@minio3 nginx]
Python test 2[root@minio3 nginx]
Python test 1[root@minio3 nginx]
``