使用 Nginx 时负载平衡不符合预期

使用 Nginx 时负载平衡不符合预期

我有一台服务器在一分钟内发送数千个请求。它连接到 Nginx 服务器。 Nginx 将根据下面的配置设置将请求上游到 4 台服务器。

upstream cmdc {
server cmdc2b:5600 max_fails=3 fail_timeout=30s;
server cmdc2a:5600 max_fails=3 fail_timeout=30s;
server cmdc1d:5600 max_fails=3 fail_timeout=30s;
server cmdc1c:5600 max_fails=3 fail_timeout=30s;
keepalive 30;
}

从 ELK 中我可以看到,几乎 50% 的请求都发送到了 cmdc2b,其他请求则分布在其他请求中。有人怀疑导致问题的其他价值观吗?我在下面列出了我的 Nginx 配置。

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
worker_rlimit_nofile 30000;
pid        /var/run/nginx.pid;


events {
    worker_connections  10240;
}


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


log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                  '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" cachestatus: $upstream_cache_status'
    'dest: $upstream_addr'
                  ' response_time: $request_time';

access_log  /var/log/nginx/access.log  main;
sendfile        off;
#tcp_nopush     on;

keepalive_timeout  65;
keepalive_requests 5000;
gzip  on;#page will be served using Nginx Gzip Module if requested by client
gzip_http_version 1.1;
gzip_vary on;
gzip_comp_level 6;
gzip_proxied any;
gzip_types text/plain text/html text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript text/x-js;
gzip_buffers 16 8k;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
#error_page--configure the server's statuses when errors occur
proxy_cache_path /opt/nds/data/nginx/cache levels=1:2 keys_zone=cmdc_cache:10m max_size=11g loader_threshold=300 loader_files=200 inactive=5m;
proxy_cache_key "$scheme$request_method$host$request_uri";
include /etc/nginx/conf.d/cmdc.conf;
}

有人可以在这里放一些光吗?

答案1

您可能会发现您正在寻找最少的连接,而不是依赖默认的循环:

upstream cmdc {
  least_conn;
  server cmdc2b:5600 max_fails=3 fail_timeout=30s;
  server cmdc2a:5600 max_fails=3 fail_timeout=30s;
  server cmdc1d:5600 max_fails=3 fail_timeout=30s;
  server cmdc1c:5600 max_fails=3 fail_timeout=30s;
  keepalive 30;
}

您可能还想对服务器应用加权,请参阅这里了解更多信息

相关内容