Django + uWSGI + nginx请求挂起

时间:2015-12-01 15:24:13

标签: django nginx docker uwsgi

我正在使用Nginx和uWSGI运行Django Web应用程序。我没有明显的原因,因为请求有问题。

我在应用程序中添加了一堆日志记录,而这个代码段似乎挂起了。在try块的开头有两个日志行,第一个被打印,但不是第二个,所以它似乎挂在代码的中间。这段代码来自我在Django配置中添加的中间件类。

def process_request(self, request):
    if 'auth' not in request.session:
        try:
            log.info("Auth not found") # this line is logged
            log.info("another log line") # this line is never logged
            if request.is_ajax():
                return HttpResponse(status=401)
            ...

我设法从uWSGI线程获得了一个回溯,这就是它被卡住的地方:

*** backtrace of 76 ***
/usr/bin/uwsgi(uwsgi_backtrace+0x2e) [0x45121e]
/usr/bin/uwsgi(what_i_am_doing+0x30) [0x451350]
/lib/x86_64-linux-gnu/libc.so.6(+0x36c30) [0x7f8a4b2b8c30]
/lib/x86_64-linux-gnu/libc.so.6(epoll_wait+0x33) [0x7f8a4b37d653]
/home/vdr/vdr-ui/env/local/lib/python2.7/site-packages/gevent/core.so(+0x27625) [0x7f8a44092625]
/home/vdr/vdr-ui/env/local/lib/python2.7/site-packages/gevent/core.so(ev_run+0x29b) [0x7f8a4409d11b]
/home/vdr/vdr-ui/env/local/lib/python2.7/site-packages/gevent/core.so(+0x32bc0) [0x7f8a4409dbc0]
/usr/lib/x86_64-linux-gnu/libpython2.7.so.1.0(PyEval_EvalFrameEx+0x4bd4) [0x7f8a4a0c30d4]
/usr/lib/x86_64-linux-gnu/libpython2.7.so.1.0(PyEval_EvalCodeEx+0x80d) [0x7f8a4a0c517d]
/usr/lib/x86_64-linux-gnu/libpython2.7.so.1.0(+0x162310) [0x7f8a4a0c5310]
/usr/lib/x86_64-linux-gnu/libpython2.7.so.1.0(PyObject_Call+0x43) [0x7f8a4a08ce23]
/usr/lib/x86_64-linux-gnu/libpython2.7.so.1.0(+0x7d30d) [0x7f8a49fe030d]
/usr/lib/x86_64-linux-gnu/libpython2.7.so.1.0(PyObject_Call+0x43) [0x7f8a4a08ce23]
/usr/lib/x86_64-linux-gnu/libpython2.7.so.1.0(PyEval_CallObjectWithKeywords+0x47) [0x7f8a4a04b837]
/home/vdr/vdr-ui/env/local/lib/python2.7/site-packages/greenlet.so(+0x375c) [0x7f8a49b1c75c]
/home/vdr/vdr-ui/env/local/lib/python2.7/site-packages/greenlet.so(+0x30a6) [0x7f8a49b1c0a6]
[0x7f8a42f26f38]
*** end of backtrace ***
SIGUSR2: --- uWSGI worker 3 (pid: 76) is managing request /login?next=/&token=45092ca6-c1a0-4c23-9d44-4d171fc561b8 since Wed Dec  2 09:52:44 2015 ---

Nginx错误日志打印出[error] 619#0: *55 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 172.17.0.1, server: vdr

uWSGI的打印输出没有错误,所以我有点不知所措。有没有人见过类似的东西?所有这些都在Docker容器中运行,如果这有任何区别。

Nginx conf:

upstream uwsgi {
    server unix:///tmp/vdr.sock;
}

server {
    listen 80;
    charset utf-8;
    client_max_body_size 500M;
    server_name localhost 172.17.0.2;

    location /static {
        alias /home/vdr/vdr-ui/static;
    }
    location / {
        include uwsgi_params;
        uwsgi_pass uwsgi;
        uwsgi_read_timeout 200s;
    }
}

uWSGI conf:

[uwsgi]

chdir = %d
module = alft_ui.wsgi:application
uid=1000
master=true
pidfile=/tmp/vdr.pid
vacuum=true
max-requests=5000
processes=4
env=DJANGO_SETTINGS_MODULE=alft_ui.settings.prod-live
home=/home/vdr/vdr-ui/env
socket=/tmp/vdr.sock
chmod-socket=666

1 个答案:

答案 0 :(得分:3)

所以我终于找到了原因。事实证明,我的安装脚本为Django配置添加了一些logstash设置。这些设置指向IP 10.8.0.1,该环境无法访问。这可以解释为什么应用程序卡在日志记录行上。删除这些设置使一切都恢复正常。

总是很高兴知道这是你自己的错误:)