在configuration了django,gunicorn,supervisor和nginx的服务器上运行非常高的stream量。 但很多时候,我倾向于看到502错误。 所以我检查了nginx日志,看看是什么错误,这是logging:
[错误] 2388#0:* 208027连接()到unix:/tmp/gunicorn-ourapp.socket失败(11:资源暂时不可用),同时连接到上游
任何人都可以帮助debugging可能会导致这种情况发生?
这是我们的nginxconfiguration:
sendfile on; tcp_nopush on; tcp_nodelay off; listen 80 default_server; server_name imp.ourapp.com; access_log /mnt/ebs/nginx-log/ourapp-access.log; error_log /mnt/ebs/nginx-log/ourapp-error.log; charset utf-8; keepalive_timeout 60; client_max_body_size 8m; gzip_types text/plain text/xml text/css application/javascript application/x-javascript application/json; location / { proxy_pass http://unix:/tmp/gunicorn-ourapp.socket; proxy_pass_request_headers on; proxy_read_timeout 600s; proxy_connect_timeout 600s; proxy_redirect http://localhost/ http://imp.ourapp.com/; #proxy_set_header Host $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 $my_scheme; #proxy_set_header X-Forwarded-Ssl $my_ssl; }
我们已经configurationDjango作为一个通用的WSGI应用程序在Gunicorn运行。 Supervisord是用来发射gunicorn工作者:
home / user / virtenv / bin / python2.7 / home / user / virtenv / bin / gunicorn –config /home/user/shared/etc/gunicorn.conf.py daggr.wsgi:application
这是gunicorn.conf.py的样子:
import multiprocessing bind = 'unix:/tmp/gunicorn-ourapp.socket' workers = multiprocessing.cpu_count() * 3 + 1 timeout = 600 graceful_timeout = 40
有谁知道我可以从哪里开始挖掘,看看有什么可能导致这个问题?
这是我的ulimit -a输出在服务器上的样子:
core file size (blocks, -c) 0 data seg size (kbytes, -d) unlimited scheduling priority (-e) 0 file size (blocks, -f) unlimited pending signals (-i) 59481 max locked memory (kbytes, -l) 64 max memory size (kbytes, -m) unlimited open files (-n) 50000 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) 1024 virtual memory (kbytes, -v) unlimited file locks (-x) unlimited
我可以通过编辑/proc/sys/net/core/somaxconn从128到20000来解决这个问题。这允许更大的stream量突发。 我可能没有必要把它设置得这么高,但是这个应用程序可以爆发得很高。 我也使用gunicorn&nginx。
就我而言,这个错误是由于我的gunicornconfiguration:
worker_class =“同步”
我使用的是:
worker_class =“gevent”#“sync”
我能用这个例子重现这个问题: https : //github.com/pawl/somaxconn_test
增加net.core.somaxconn最终修复它。
如果它不是docker容器,可以用sysctl -w net.core.somaxconn=<your value> 。 如果它是一个docker容器,你可以使用这个标志:– --sysctl net.core.somaxconn=1024
这听起来像是因为所有的枪炮工都在使用。 我会暂时打开gunicornlogin。 请参阅这里的日志设置 。 这应该让你看到gunicorn工人的状态,以及为什么在502发生时不能build立新的连接。