504网关超时uwsgi + nginx django应用程序

时间:2015-09-28 06:36:50

标签: django nginx uwsgi

我正在尝试使用Nginx + uwsgi运行我的Django应用程序,但是在加载一分钟后我收到504 Gateway Time-out

我的应用程序需要时间来完成所需的操作,因为它会搜索多个网站上的特定内容。

我的nginx conf是下一个:

upstream uwsgi {
    server 127.0.0.1:8000;
}

server {

    listen 80;
    server_name server_ip;

    root /opt/emails/subscriptions;
    index index.html index.htm index.php;

    location /emailsproject/ {
        root /opt/emails/subscriptions/;
    }

    location / {
        proxy_set_header X-Real-IP      $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://uwsgi;
        proxy_set_header Host $http_host;
        uwsgi_read_timeout 18000;
    }
}

我的uwsgi脚本:

description "uWSGI server"

env PYTHONPATH=/opt/emails/subscriptions
env DJANGO_SETTINGS_MODULE=emailsproject.settings

start on runlevel [2345]
stop on runlevel [!2345]
respawn
exec uwsgi_python --http-socket  127.0.0.1:8000 -p 4 --wsgi-file /opt/emails/subscriptions/emailsproject/wsgi.py

我的nginx在error.log中给出了followin错误消息:

2015/09/28 02:15:57 [error] 4450#0: *19 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 37.235.53.246, server: my_server_ip, request: "POST /home/ HTTP/1.1", upstream: "http://127.0.0.1:8000/home/", host: "my_server_ip", referrer: "http://my_server_ip/home/"

有没有人知道如何摆脱这个?我已经尝试了大量的stackoverflows解决方案,但没有一个对我有用。

2 个答案:

答案 0 :(得分:4)

如果内部任务需要花费太多时间进行处理,请使用芹菜来运行任务。 http://docs.celeryproject.org/en/latest/userguide/tasks.html

如果它不是纯粹的内部任务,例如: - 上传大文件,然后将Nginx client_body_timeout增加到大于60s

因为nginx配置中的默认超时。编辑Nginx虚拟主机文件 并在 server{} 部分添加以下行。 http://nginx.org/en/docs/http/ngx_http_core_module.html#client_body_timeout

# defualt is 60 seconds, For a 300 second timeout. 
client_body_timeout 300s;

编辑:   uwsgi_read_timeout 300s; 也是必需的。但它已经在你的配置中了。

答案 1 :(得分:0)

晚聚会。我花了几个小时配置nginx.cfg,最终意识到它与EC2服务器的负载平衡器设置有关。如果您将AWS Load Balancer用于EC2服务器,请尝试同时增加Idle timeout和nginx.cfg的更改。