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
归档时间: |
|
查看次数: |
32933 次 |
最近记录: |