我正在运行一个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; } }
一些应用程序服务器进程反过来将请求从/tmp/app.sock
提取出来。 这里使用的特定应用程序服务器是Unicorn,但我不认为这与这个问题有关。
问题是,似乎只是经过了一定的负载,nginx无法以足够快的速度通过套接字获取请求。 不pipe我设置了多less个应用程序服务器进程,
我在nginx错误日志中收到了这些消息的洪水:
connect() to unix:/tmp/app.sock failed (11: Resource temporarily unavailable) while connecting to upstream
许多请求导致状态码502,而那些需要很长时间才能完成的请求。 nginx写队列统计在1000左右。
无论如何,我觉得我在这里丢失了一些明显的东西,因为nginx和app服务器的这种特殊configuration是相当普遍的,特别是对于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
当前的内核调整:
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
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
这听起来像瓶颈是应用程序供电的sockets,而不是Nginx本身。 与套接字和TCP / IP连接一起使用时,我们看到了很多PHP。 在我们的例子中,PHP的瓶颈比Nginx早得多。
你检查了sysctl.conf连接跟踪限制,套接字积压限制
net.core.somaxconn
net.core.netdev_max_backlog
您可以尝试查看unix_dgram_qlen
,请参阅proc文档 。 虽然这可能会通过指出更多的队列来解决问题? 你必须看(netstat -x …)
我通过增加config / unicorn.rb中的积压数来解决问题…我以前的积压是64。
listen "/path/tmp/sockets/manager_rails.sock", backlog: 64
我得到这个错误:
2014/11/11 15:24:09 [error] 12113#0: *400 connect() to unix:/path/tmp/sockets/manager_rails.sock failed (11: Resource temporarily unavailable) while connecting to upstream, client: 192.168.101.39, server: , request: "GET /welcome HTTP/1.0", upstream: "http://unix:/path/tmp/sockets/manager_rails.sock:/welcome", host: "192.168.101.93:3000"
现在,我增加到1024,我没有得到错误:
listen "/path/tmp/sockets/manager_rails.sock", backlog: 1024
在独angular兽configuration中积压默认值是1024。
http://unicorn.bogomips.org/Unicorn/Configurator.html
listen "/path/to/.unicorn.sock", :backlog => 1024
1024客户端是unix域套接字限制。