需要将 nginx 吞吐量增加到上游 unix 套接字——Linux 内核调整?

Ben*_*Lee 29 linux tuning nginx socket unicorn

我正在运行一个 nginx 服务器,它充当上游 unix 套接字的代理,如下所示:

upstream app_server {
        server unix:/tmp/app.sock fail_timeout=0;
}

server {
        listen ###.###.###.###;
        server_name whatever.server;
        root /web/root;

        try_files $uri @app;
        location @app {
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Proto $scheme;
                proxy_set_header Host $http_host;
                proxy_redirect off;
                proxy_pass http://app_server;
        }
}
Run Code Online (Sandbox Code Playgroud)

一些应用服务器进程反过来在请求/tmp/app.sock可用时将其拉出。此处使用的特定应用服务器是 Unicorn,但我认为这与此问题无关。

问题是,似乎超过一定量的负载后,nginx 无法以足够快的速度通过套接字获取请求。我设置了多少个应用服务器进程并不重要。

我在 nginx 错误日志中收到了大量这些消息:

connect() to unix:/tmp/app.sock failed (11: Resource temporarily unavailable) while connecting to upstream
Run Code Online (Sandbox Code Playgroud)

许多请求会导致状态代码 502,以及那些不需要很长时间才能完成的请求。nginx 写入队列统计数据徘徊在 1000 左右。

无论如何,我觉得我在这里遗漏了一些明显的东西,因为 nginx 和应用程序服务器的这种特殊配置非常普遍,尤其是对于 Unicorn(实际上这是推荐的方法)。是否有任何需要设置的 linux 内核选项,或者 nginx 中的某些内容?关于如何增加上游套接字的吞吐量的任何想法?我显然做错了什么?

关于环境的其他信息:

$ uname -a
Linux servername 2.6.35-32-server #67-Ubuntu SMP Mon Mar 5 21:13:25 UTC 2012 x86_64 GNU/Linux

$ ruby -v
ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-linux]

$ unicorn -v
unicorn v4.3.1

$ nginx -V
nginx version: nginx/1.2.1
built by gcc 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5)
TLS SNI support enabled
Run Code Online (Sandbox Code Playgroud)

当前内核调整:

net.core.rmem_default = 65536
net.core.wmem_default = 65536
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_rmem = 4096 87380 16777216
net.ipv4.tcp_wmem = 4096 65536 16777216
net.ipv4.tcp_mem = 16777216 16777216 16777216
net.ipv4.tcp_window_scaling = 1
net.ipv4.route.flush = 1
net.ipv4.tcp_no_metrics_save = 1
net.ipv4.tcp_moderate_rcvbuf = 1
net.core.somaxconn = 8192
net.netfilter.nf_conntrack_max = 524288
Run Code Online (Sandbox Code Playgroud)

nginx 用户的 Ulimit 设置:

core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 20
file size               (blocks, -f) unlimited
pending signals                 (-i) 16382
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 65535
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) unlimited
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited
Run Code Online (Sandbox Code Playgroud)

Ben*_*ani 17

听起来瓶颈在于为套接字供电的应用程序,而不是 Nginx 本身。当与套接字与 TCP/IP 连接一起使用时,我们在 PHP 中经常看到这一点。在我们的例子中,PHP 瓶颈比 Nginx 早得多。

您是否检查过 sysctl.conf 连接跟踪限制、套接字积压限制

  • net.core.somaxconn
  • net.core.netdev_max_backlog

  • 我解决了这个问题。请参阅我发布的答案。它实际上*是*应用程序瓶颈,而不是套接字,正如您所假设的那样。由于误诊,我早些时候已经排除了这一点,但事实证明问题是到另一台服务器的吞吐量。几个小时前才弄清楚这一点。我将奖励您,因为即使我在问题中提出了错误诊断,您也几乎确定了问题的根源;但是,要给我的答案打勾,因为我的答案描述了确切的情况,因此可能会帮助将来遇到类似问题的人。 (2认同)

小智 2

您可以尝试查看 unix_dgram_qlenproc文档。尽管这可能会因为在队列中指向更多内容而使问题变得更加复杂?你必须看看(netstat -x ...)