Nginx循环负载平衡不符合预期

Nginx循环负载平衡不符合预期,nginx,nginx-reverse-proxy,nginx-config,Nginx,Nginx Reverse Proxy,Nginx Config,我们正在连接一个系统,其中暴露了4个端口以满足grpc请求。使用nginx作为负载平衡器转发4个客户端grpc请求,配置如下: user www-data; worker_processes auto; pid /run/nginx.pid; include /etc/nginx/modules-enabled/*.conf; events { worker_connections 768; # multi_accept on; } http { lo

我们正在连接一个系统,其中暴露了4个端口以满足grpc请求。使用nginx作为负载平衡器转发4个客户端grpc请求,配置如下:

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
        worker_connections 768;
        # multi_accept on;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent"';

    map $http_upgrade $connection_upgrade {
        default upgrade;
        ''      close;
    }
    upstream backend{
        #least_conn;
        server localhost:9000 weight=1 max_conns=1;
        server localhost:9001 weight=1 max_conns=1;
        server localhost:9002 weight=1 max_conns=1;
        server localhost:9003 weight=1 max_conns=1;
        }

    server {
        listen 80 http2;

        access_log /tmp/access.log main;
        error_log /tmp/error.log error;

        proxy_buffering off;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Scheme $scheme;
        proxy_set_header Host $http_host;

        location / {
                #eepalive_timeout 0;
                grpc_pass grpc://backend;
                grpc_pass_header userid;
                grpc_pass_header transid;
        }
    }
}
据观察,几乎所有客户端4请求都会发送到所有4个端口,但有时(比如30%)只发送到2个端口/3个端口。似乎NGINX并没有像预期的那个样进行默认循环。我们尝试了所有的可能性,比如马克斯康斯,最少康斯,体重,但没有运气

似乎我遇到了以下链接中的问题:

https://serverfault.com/questions/895116/nginx-round-robin-nor-exactly-round-robin
https://stackoverflow.com/questions/40859396/how-to-test-load-balancing-in-nginx
当我浏览Quora时,发现nginx中的“fair”模块可以解决这个问题

    "The Nginx fair proxy balancer enhances the standard round-robin load 
    balancer provided with Nginx so that it will track busy back end servers (e.g. Thin, Ebb, Mongrel) and balance the load to non-busy server processes. "

https://www.quora.com/What-is-the-best-way-to-get-Nginx-to-do-smart-load-balancing

我尝试从源代码使用NGINX的“fair”模块,但遇到了很多问题。我无法启动NGINX本身。有人能帮我们解决这个问题吗?

我们找到了答案!!!!刚刚将“worker\u processs auto”更改为“worker\u processs 1”;现在,它工作正常


所有请求都正确地进行了负载平衡。在这里,我们认为如果您使用的不是单个工作者,则多个工作者可能会将请求发送到同一端口。

我不知道为什么会发生这种情况,但可能与浏览器有关。
我在使用浏览器发送请求时遇到了相同的问题。当我使用curl从终端发送请求时,它工作正常。

您尝试了多少次?我们尝试了50多次。我们尝试改变所有可能的组合,如最小连接、重量、保持连接超时、最大连接。结果总是在60%到70%之间(负载平衡正常)。@einverne:负载平衡可以很好地处理http请求。这可能是grpc/http2协议的问题吗?您需要在
上游
部分下指定一个共享内存
区域
,以便工作人员可以共享状态-我也有同样的问题,直到您重新阅读
最大连接
后才清楚“如果服务器组不驻留在共享内存中,则限制对每个工作进程有效”。它对您有效,因为您现在只有1个工作进程,不需要共享状态,但如果将其更改为例如2或
auto
,则在添加共享内存区域之前,您将遇到相同的问题。