使用 ip_hash 指令进行 NGINX 负载平衡

使用 ip_hash 指令进行 NGINX 负载平衡

我有一个简单的双节点服务器集群,在localhost:8001和上运行localhost:8002,使用 NGINX 进行负载平衡。下面是我的 http 上下文nginx.conf

http {
    include       mime.types;
    default_type  application/octet-stream;

    upstream backend {
        ip_hash;
        server localhost:8001;
        server localhost:8002;
    }

    log_format upstreamlog 'upstream: $upstream_addr: $request upstream-response-status: $upstream_status';

    server {
        listen              80;
        listen              [::]:80;
        server_name         localhost;
        access_log  logs/access.log  upstreamlog;

        location / {
            proxy_pass http://backend/;
        }
    }
}

最初所有请求http://本地主机/被重定向到在端口 8001 上运行的上游服务器。

--日志

 upstream: [::1]:8001: GET / HTTP/1.1 upstream-response-status: 200
 upstream: [::1]:8001: GET /favicon.ico HTTP/1.1 upstream-response-status: 200
 upstream: [::1]:8001: GET / HTTP/1.1 upstream-response-status: 200
 upstream: [::1]:8001: GET /favicon.ico HTTP/1.1 upstream-response-status: 200
 upstream: [::1]:8001: GET / HTTP/1.1 upstream-response-status: 200
 upstream: [::1]:8001: GET /favicon.ico HTTP/1.1 upstream-response-status: 200
 upstream: [::1]:8001: GET / HTTP/1.1 upstream-response-status: 200
 upstream: [::1]:8001: GET /favicon.ico HTTP/1.1 upstream-response-status: 200
 ----
 upstream: [::1]:8001: GET / HTTP/1.1 upstream-response-status: 200
 upstream: [::1]:8001: GET /favicon.ico HTTP/1.1 upstream-response-status: 200
 upstream: [::1]:8001: GET / HTTP/1.1 upstream-response-status: 200
 upstream: [::1]:8001: GET /favicon.ico HTTP/1.1 upstream-response-status: 200
 upstream: [::1]:8001: GET / HTTP/1.1 upstream-response-status: 200
 upstream: [::1]:8001: GET /favicon.ico HTTP/1.1 upstream-response-status: 200
 upstream: [::1]:8001: GET / HTTP/1.1 upstream-response-status: 200
 upstream: [::1]:8001: GET /favicon.ico HTTP/1.1 upstream-response-status: 200

现在,为了测试此设置的故障转移,我停止了在端口 8001 上运行的服务器。但故障转移不起作用,所有后续请求也被转发到端口 8001 的服务器。

--日志

 upstream: [::1]:8001: GET / HTTP/1.1 upstream-response-status: -
 upstream: [::1]:8001: GET / HTTP/1.1 upstream-response-status: -
 upstream: [::1]:8001: GET / HTTP/1.1 upstream-response-status: -
 upstream: [::1]:8001: GET / HTTP/1.1 upstream-response-status: -
 upstream: [::1]:8001: GET / HTTP/1.1 upstream-response-status: -
 upstream: [::1]:8001: GET / HTTP/1.1 upstream-response-status: -
 upstream: [::1]:8001: GET / HTTP/1.1 upstream-response-status: -
 ----
 upstream: [::1]:8001: GET / HTTP/1.1 upstream-response-status: -
 upstream: [::1]:8001: GET / HTTP/1.1 upstream-response-status: -
 upstream: [::1]:8001: GET / HTTP/1.1 upstream-response-status: -
 upstream: [::1]:8001: GET / HTTP/1.1 upstream-response-status: -
 ----
 upstream: [::1]:8001: GET / HTTP/1.1 upstream-response-status: -
 upstream: [::1]:8001: GET / HTTP/1.1 upstream-response-status: -
 upstream: [::1]:8001, 127.0.0.1:8001, [::1]:8002: GET / HTTP/1.1 upstream-response-status: 504, 504, 200

NGINX 花了很长时间(大约 3 分钟)切换到端口 8002 上的另一个节点。我在配置中遗漏了什么?我知道默认值max_fails1并且fail_timeout10 seconds。如何让 NGINX 切换到其他服务器节点且零停机时间?

(注意:ip_hash必须用于会话亲和性和其他目的)

答案1

我认为您需要在 location 块中添加 proxy_next_upstream 指令。此指令功能是指定在哪些情况下应将请求传递到下一个服务器。然后添加 http_503,因为当您停止实例时,它会抛出 503 或服务不可用。如果您的问题是由于超时,您可以更改 proxy_connect_timeout 和 proxy_read_timeout。示例配置

    location / {
        proxy_pass http://backend/;
        proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
        #timeout for 10 second
        proxy_connect_timeout 10;
        proxy_read_timeout 10;
    }

以下是所有代理指令的文档http://nginx.org/en/docs/http/ngx_http_proxy_module.html

相关内容