【问题标题】:NGINX 502 under high load with NodeJSNGINX 502 在高负载下使用 NodeJS
【发布时间】:2017-07-11 15:47:31
【问题描述】:

这是我网站的 nginx 配置文件:

server {
    listen 80;
    listen [::]:80;

    server_name domain.com;

    proxy_http_version 1.1;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
    proxy_intercept_errors on;

    location / {
        proxy_pass http://127.0.0.1:10000;
    }
}

在高负载性能测试 (1200 RPS) 下使用 nginx 时,我得到 50% 的错误率,它返回为 502。我在 nodejs 中的应用程序中没有错误日志,因为它被 nginx 直接拒绝了。

如果我修改 iptables 以将端口 80 重定向到 10000(应用程序端口),我得到零错误;和更好的性能。

我想避免这种情况,因为将来我会使用 let's encrypt 和 nginx 来服务 https;但我需要保持原始性能。

我已经尝试过寻找答案,但在 PHP 上看到了一些,但在 node 上却没有。

有没有人遇到过这个问题或者知道如何微调 nginx? nginx.conf 现在设置为默认安装。

nginx.conf

user www-data;
worker_processes auto;
pid /run/nginx.pid;

events {
    worker_connections 768;
    # 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_disable "msie6";

    # 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/*;
}


#mail {
#   # See sample authentication script at:
#   # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
# 
#   # auth_http localhost/auth.php;
#   # pop3_capabilities "TOP" "USER";
#   # imap_capabilities "IMAP4rev1" "UIDPLUS";
# 
#   server {
#       listen     localhost:110;
#       protocol   pop3;
#       proxy      on;
#   }
# 
#   server {
#       listen     localhost:143;
#       protocol   imap;
#       proxy      on;
#   }
#}

更新

除了遵循本指南 (https://www.linode.com/docs/websites/nginx/configure-nginx-for-optimized-performance),我还必须:

编辑 sysctl.conf 并添加以下内容:

sysctl net.core.somaxconn=65536
sysctl net.ipv4.tcp_max_tw_buckets=1440000
sysctl net.ipv4.ip_local_port_range=1024 65000
sysctl net.ipv4.tcp_fin_timeout=15
sysctl net.ipv4.tcp_window_scaling=1    
sysctl net.ipv4.tcp_max_syn_backlog=3240000

编辑 /etc/security/limits.conf 并添加:

www-data    hard    nofile    65536
www-data    soft    nofile    65536

编辑 lib/systemd/system/nginx.service 配置并在 KillMode 下添加以下内容

LimitNOFILE=65536

为了性能,我将 /etc/nginx/proxy_params 替换为:

proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;

proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_cache_bypass $http_upgrade;
proxy_intercept_errors on;

proxy_redirect off;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
proxy_temp_path /etc/nginx/proxy_temp;

【问题讨论】:

  • 你也可以发布主配置文件吗?
  • 已更新主配置。
  • 我真的没有时间深入研究这个,但我对结果很好奇。您是否尝试取消注释 multi_accept on; 行?一些优化指南建议这样做。
  • 如果这些是短暂的请求,也可能是您的"Ephemeral Ports" 用完了。尝试在此处更改该标题下的设置:linode.com/docs/websites/nginx/…
  • @david 这比我想象的要复杂一些,但你走在正确的轨道上。我做了你给出的链接所说的所有事情(除了静态项目),但除此之外我还必须做以下事情。我正在将更新添加到原始帖子。

标签: node.js nginx performance-testing


【解决方案1】:

供以后参考。 关闭access_log off 的访问日志以进行生产。

这几乎不会提高性能。

【讨论】:

    猜你喜欢
    • 2014-03-27
    • 2012-02-05
    • 1970-01-01
    • 2013-11-30
    • 2011-10-09
    • 1970-01-01
    • 1970-01-01
    • 2018-09-10
    • 2017-03-29
    相关资源
    最近更新 更多