【问题标题】:504 Gateway Time-out uwsgi + nginx django application504 网关超时 uwsgi + nginx django 应用程序
【发布时间】:2015-12-25 08:09:40
【问题描述】:

我正在尝试使用 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 中给了我以下错误消息:

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 解决方案,但没有一个对我有用。

【问题讨论】:

  • 如果您的应用程序正在运行一个较长的进程,您可以尝试使用 celery 异步运行它。 Nginx 可能有办法增加超时时间,但是长时间同步任务并不是一个好主意。 celery.readthedocs.org/en/latest/django/…
  • 你找到解决办法了吗?

标签: django nginx uwsgi


【解决方案1】:

如果它是一个需要太多时间处理的内部任务,请使用 celery 来运行该任务。 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

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

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

【讨论】:

    【解决方案2】:

    聚会迟到了。我花了几个小时配置 nginx.cfg 并最终意识到它与我的 EC2 服务器的负载均衡器设置有关。如果您为 EC2 服务器使用 AWS 负载均衡器,请尝试同时增加 Idle timeout 以及 nginx.cfg 的更改。

    【讨论】:

      【解决方案3】:

      我的应用程序环境是 Django/Python/Nginx,它不在 AWS 上。我发现了许多线索,他们指出修改了这些参数并应用了它们,但在我尝试修改 /etc/nginx/sites-available/ 中的一个名为“default”的文件并添加以下行之前,没有一个对我有用: uwsgi_read_timeout 3600;

      我在“listen 443”的位置区域内做了这个修改,如下图:

      服务器 { 听着 443... ... 地点 / { uwsgi_pass django; ... uwsgi_read_timeout 3600; } ...

      【讨论】:

        猜你喜欢
        • 2011-10-04
        • 2015-02-09
        • 2019-09-29
        • 1970-01-01
        • 2018-05-17
        • 2011-08-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多