当 NginX 服务器关闭时会发生什么?

what happens when NginX server is shut down?

假设我们有一个 Web 应用程序 运行 在三个不同的服务器中。我们有一个 NginX 服务器,它有一个负载平衡器,并将请求重定向到这三个服务器。
那么当 NginX 服务器不再 运行 时,请求会发生什么情况?他们是否重定向到其中一台服务器?如果不是,我们如何将它们重定向到其中一台服务器?

如果其中一个负载平衡实例出现故障,请求仍会被路由到该服务器,因为 nginx 无法知道上游实例出现故障。三分之一的请求您将获得 502 Bad Gateway

为了避免宕机服务器获取请求,可以使用nginx的health checks

NGINX and NGINX Plus can continually test your upstream servers, avoid the servers that have failed, and gracefully add the recovered servers into the load‑balanced group.

在您的应用中,您可以有一个路径 /health_check,如果实例正常并使用此配置,它会以 200 状态代码响应。

http {
    upstream backend {
        server backend1.example.com;
        server backend2.example.com;
    }
    server {
        location / {
            proxy_pass http://backend;
            health_check uri=/health_check;
        }
    }
}