Nginx 轮询负载均衡未达到预期

Mah*_*esh 5 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 {
    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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

据观察,少数情况下所有客户端 4 请求都会发送至所有 4 个端口,但有时(例如 30%)仅发送至 2 个端口/3 个端口。NGINX 似乎没有按预期发生默认循环。我们尝试了所有可能性,例如 max_conns、least_conn、weight,但没有成功。

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

https://serverfault.com/questions/895116/nginx-round-robin-nor-exactly-round-robin
/sf/ask/2860157751/
Run Code Online (Sandbox Code Playgroud)

当我浏览 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
Run Code Online (Sandbox Code Playgroud)

我尝试从源代码中使用 NGINX 的“公平”模块,但遇到了很多问题。我无法启动 NGINX 本身。任何人都可以帮助解决这个问题吗?

Mah*_*esh 4

我们得到了答案!!!刚刚更改了“worker_processes auto;” 到“worker_processes 1;” 现在,它运行良好。

所有请求均已正确负载平衡。在这里,我们认为如果您使用单个工作人员以外的其他工作人员,多个工作人员可能会将请求发送到同一端口。