{"msg":"操作成功","code":200,"data":{"createBy":"admin","createTime":"2025-05-20 21:18:31","updateBy":"admin","updateTime":"2025-06-18 21:32:38","remark":null,"id":112,"articleTitle":"Nginx（五）健康监测机制","articleUrl":"nginx_health_check","articleThumbnail":"https://www.asumimoe.com/imgfiles/20250618/4b57c2e46f8949e5a30d4e2e9e8a8fd5.png","articleFlag":"0","draftStatus":"1","reprintStatement":"1","articleSummary":"在当今互联网应用中，高并发和高可用性是非常重要的。为了保障系统的稳定性，我们需要对服务器的健康状况进行监控和检查，并能够自动进行故障恢复。","articleContent":"在当今互联网应用中，高并发和高可用性是非常重要的。为了保障系统的稳定性，我们需要对服务器的健康状况进行监控和检查，并能够自动进行故障恢复。\n\n在Nginx配置文件中，我们可以通过upstream模块来定义一组后端服务器，Nginx会根据一定的算法为请求分配到不同的服务器上。在这个过程中，Nginx可以通过检查后端服务器的健康状况来动态调整请求的分配。常用的健康检查方式有被动健康检查和主动健康检查两种。\n\n## 被动健康检查\n\n当Nginx向后端服务器发送请求且连接超时或者请求失败时，Nginx会判断该服务器为不可用服务器并将其从可用服务器列表中移除。当有新的请求到达时，Nginx会尝试连接其他可用服务器。\n\n后端服务器被标记为不可用的条件是为每个后端服务器定义的，并在`upstream`中使用`server`指令的参数：\n\n- [`fail_timeout`](https://nginx.org/en/docs/http/ngx_http_upstream_module.html#fail_timeout) – 设置服务器标记为不可用时必须发生多次失败尝试的时间，以及服务器标记为不可用的时间（默认值为 10 秒）。\n- [`max_fails`](https://nginx.org/en/docs/http/ngx_http_upstream_module.html#max_fails) – 设置在服务器标记为不可用的期间内必须发生的失败尝试次数（默认为 1 次尝试）。\n\n在以下示例中，如果 NGINX 无法向服务器发送请求，或者在 30 秒内 3 次未收到来自服务器的响应，则会在 30 秒内将服务器标记为不可用：\n\n```bash\nupstream backend {\n    server backend1.example.com;\n    server backend2.example.com max_fails=3 fail_timeout=30s;\n}\n```\n\n需要重点注意的是 fails 是一个区间内失败的累加值，也就是在 fail_timeout 的这个时间区间内，两个错误之间即使有成功的请求，fails 也依然会进行累加计算。更为精确的表述这个功能细节如下：\n\n1）如果在 T0 时刻出现了一个错误，那么 fails = 1；\n\n2）在 T0 ~ T0 + fail_timeout 的时间区间内：\n\n​\t如果没有出现错误，那么 fails 会重置为 0；\n\n​\t如果出现了一个新错误，比如在 T1 时刻出现一个新的错误，那么 fails 会继续累加，fails = 2\n\n3）接着会重新以新的时间区间 T1 ~ T1 + fail_timeout 开始统计但是 fails 值却是继续累加，如果这个时间区间又有一个新的错误，那么 fails = 3\n\n4）直到 T2 时刻，出现了新的错误并 fails >= max_fails，那么 peer 节点会被摘除，在 T2 ~ T2 + fail_timeout 这个时间内，节点就无法对外提供服务，并且重置 fails 为 0，然后开启新的一轮检测\n\n## 主动健康检查\n\nNGINX Plus可以通过向每个服务器发送特殊的健康检查请求并验证正确的响应来定期检查后端服务器的运行状况。\n\n要启用主动健康检查，请执行以下操作：\n\n1. 在`location`配置中加入`health_check`配置参数：\n\n   ```bash\n   server {\n       location / {\n           proxy_pass http://backend;\n           health_check;\n       }\n   }\n   ```\n\n   此代码段定义了一个服务器，该服务器将所有请求 （） 传递给名为 的上游组。它还使用 [`health_check`](https://nginx.org/en/docs/http/ngx_http_upstream_hc_module.html#health_check) 指令启用高级运行状况监控：默认情况下，NGINX Plus 每 5 秒向组中的每台服务器发送一次“**/**”请求。如果发生任何通信错误或超时（服务器使用超出 from through 范围的状态代码进行响应），则运行状况检查将失败。服务器被标记为不正常，NGINX Plus 不会向其发送客户端请求，直到它再次通过运行状况检查。`location /``backend``backend``200``399`\n\n   或者，您可以指定另一个端口进行运行状况检查，例如，用于监视同一主机上许多服务的运行状况。使用 [`health_check`](https://nginx.org/en/docs/http/ngx_http_upstream_hc_module.html#health_check) 指令的 [`port`](https://nginx.org/en/docs/http/ngx_http_upstream_hc_module.html#health_check_port) 参数指定新端口：\n\n   ```bash\n   server {\n       location / {\n           proxy_pass   http://backend;\n           health_check port=8080;\n       }\n   }\n   ```\n\n2. 在上游服务器组中，使用 [`zone`](https://nginx.org/en/docs/http/ngx_http_upstream_module.html#zone) 指令定义共享内存区域：\n\n   ```bash\n   http {\n       upstream backend {\n           zone backend 64k;\n           server backend1.example.com;\n           server backend2.example.com;\n           server backend3.example.com;\n           server backend4.example.com;\n       }\n   }\n   ```\n\n   该区域在所有工作进程之间共享，并存储上游组的配置。[这使工作](https://docs.nginx.com/nginx/admin-guide/load-balancer/http-load-balancer/#shared)进程能够使用同一组计数器来跟踪来自组中服务器的响应。\n\n   可以使用指令的参数覆盖活动运行状况检查的默认值：`health_check`\n\n   ```bash\n   location / {\n       proxy_pass   http://backend;\n       health_check interval=10 fails=3 passes=2;\n   }\n   ```\n\n   在这里，[`interval`](https://nginx.org/en/docs/http/ngx_http_upstream_hc_module.html#health_check_interval) 参数将运行状况检查之间的延迟从默认的 5 秒增加到 10 秒。[`fails`](https://nginx.org/en/docs/http/ngx_http_upstream_hc_module.html#health_check_fails) 参数要求服务器未通过三次运行状况检查才能标记为不正常（高于默认运行状况检查）。最后，[`passes`](https://nginx.org/en/docs/http/ngx_http_upstream_hc_module.html#health_check_passes) 参数表示服务器必须通过两次连续检查才能再次标记为正常，而不是默认检查。\n\n   您还可以使用 [`keepalive_time`](https://nginx.org/en/docs/http/ngx_http_upstream_hc_module.html#health_check_keepalive_time) 参数启用连接缓存 - 在 `TLS` 上游的情况下，不会对每个运行状况检查探测进行完整的 `TLS` 握手，并且可以在指定的时间段内重用连接：\n\n   ```bash\n   location / {\n       proxy_http_version 1.1;\n       proxy_set_header   Connection \"\";\n       proxy_pass         https://backend;\n       health_check       interval=1 keepalive_time=60s;\n   }\n   ```","categoryId":12,"viewCount":135,"categoryName":"Nginx","author":"球接子","authorAvatar":null,"tagIds":[14,13],"tagNames":["中间件","Nginx"]}}