NGINX/uWSGI 负载下的反向代理 502 错误网关
NGINX/uWSGI reverse proxy 502 Bad Gateway under load
我正在使用 NGINX 作为 uWSGI 服务器的反向代理 运行 Flask 应用程序。大多数请求都已成功处理,但在 500 个并发连接的负载测试后,一些请求(但不是全部!)被丢弃并出现 502 Bad Gateway 错误。
根据错误日志,似乎 NGINX 无法将连接发送到 uWSGI。然而,我不知道为什么会失败,因为我很容易 运行 100 个 uWSGI 进程,每个进程都有 300 个监听积压。
我已经将 net.core.somaxconn 调整为 4096。我的 nginx.conf 看起来像这样:
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
worker_rlimit_nofile 65535;
events {
worker_connections 65535;
multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# SSL Settings
##
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
我的站点可用配置如下所示(将真正的 url 替换为 myservice.com):
server {
server_name myservice.com www.myservice.com;
client_header_buffer_size 64k;
large_client_header_buffers 8 64k;
location / {
include uwsgi_params;
uwsgi_pass unix:/home/ubuntu/api_master/api.sock;
client_max_body_size 500M;
}
listen 443 ssl backlog=65535; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/myservice.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/myservice.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
if ($host = www.myservice.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
if ($host = myservice.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
server_name myservice.com www.myservice.com;
return 404; # managed by Certbot
}
不胜感激!如果我能在这台服务器上达到 4,096+ 个并发连接,那就太好了;我相信硬件可以处理它。
我建议您先尝试将 net.core.somaxconn 值乘以 4,然后看看情况是否会好转。否则,我会怀疑 multi_accept on;
!
找到解决方案!事实证明这是 uWSGI 的问题,而不是 NGINX。我错误的假设是监听积压是每个 进程 ,而不是汇总,增加该限制解决了问题。
我正在使用 NGINX 作为 uWSGI 服务器的反向代理 运行 Flask 应用程序。大多数请求都已成功处理,但在 500 个并发连接的负载测试后,一些请求(但不是全部!)被丢弃并出现 502 Bad Gateway 错误。
根据错误日志,似乎 NGINX 无法将连接发送到 uWSGI。然而,我不知道为什么会失败,因为我很容易 运行 100 个 uWSGI 进程,每个进程都有 300 个监听积压。
我已经将 net.core.somaxconn 调整为 4096。我的 nginx.conf 看起来像这样:
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
worker_rlimit_nofile 65535;
events {
worker_connections 65535;
multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# SSL Settings
##
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
我的站点可用配置如下所示(将真正的 url 替换为 myservice.com):
server {
server_name myservice.com www.myservice.com;
client_header_buffer_size 64k;
large_client_header_buffers 8 64k;
location / {
include uwsgi_params;
uwsgi_pass unix:/home/ubuntu/api_master/api.sock;
client_max_body_size 500M;
}
listen 443 ssl backlog=65535; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/myservice.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/myservice.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
if ($host = www.myservice.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
if ($host = myservice.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
server_name myservice.com www.myservice.com;
return 404; # managed by Certbot
}
不胜感激!如果我能在这台服务器上达到 4,096+ 个并发连接,那就太好了;我相信硬件可以处理它。
我建议您先尝试将 net.core.somaxconn 值乘以 4,然后看看情况是否会好转。否则,我会怀疑 multi_accept on;
!
找到解决方案!事实证明这是 uWSGI 的问题,而不是 NGINX。我错误的假设是监听积压是每个 进程 ,而不是汇总,增加该限制解决了问题。