大网数据广告活动
服务器运维问答

NGINX load balancing using ip_hash directive

· 52CE 我爱测

I have a simple two node server cluster, running on localhost:8001 and localhost:8002 , load-balanced using NGINX. Below is the http context of my nginx.conf . http { include mime.types; default_type application/octet-stream; ups…

问题原文

原帖链接 · mukut bhattacharjee · CC BY-SA 3.0

I have a simple two node server cluster, running on localhost:8001 and localhost:8002, load-balanced using NGINX. Below is the http context of my 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/;
        }
    }
}

Initially all requests to http://localhost/ were redirected to upstream server running at port 8001.

--Logs

 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

Now for testing the fail-over of this setup, I stopped the server running at port 8001. But the fail-over did not work and all subsequent requests were also forwarded to the server at port 8001.

--Logs

 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 took a long time, approximately 3 minutes, for switching over to the other node at port 8002. What is that I am missing in the configuration? I know that default max_fails is 1 and fail_timeout is 10 seconds. How to make NGINX switch-over to other server node with zero downtime?

(NOTE: ip_hash had to be used for session affinity and other purposes)

已采纳解答

原帖链接 · Ilham Sulaksono · CC BY-SA 3.0

I think you need to add proxy_next_upstream directive in location block. This directive function is to specify in which cases a request should be passed to the next server. Then add http_503, because when you stop the instance it will throw 503 or service unavailable. If your problem is because timeout you can change the proxy_connect_timeout and proxy_read_timeout. Example configuration

    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;
    }

Here is the documentation for all proxy directive is in http://nginx.org/en/docs/http/ngx_http_proxy_module.html

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2018-01-11)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。