【问题标题】:Nginx 504 Gateway Timeout Error for DjangoDjango 的 Nginx 504 网关超时错误
【发布时间】:2016-06-19 01:20:28
【问题描述】:

我正在使用 1ClickInstallation 图像在 DigitalOcean 上运行 Django 站点。一切正常,但我遇到了 504 网关超时错误的问题。我在博客上尝试了多种设置,但无法正常工作。以下是我的设置:

upstream app_server {
    server 127.0.0.1:9000 fail_timeout=0;

}


server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /home/django/django_project;
    index index.html index.htm;

    client_max_body_size 4G;
    server_name www.mydomain.com;


    keepalive_timeout 5;

    location ~*  \.(jpg|jpeg|png|gif|ico|css|js|woff2|woff|ttf)$ {
        expires 365d;
    }



    # Your Django project's media files - amend as required
    location /media  {
        alias /home/django/django_project/media/;
    }

    # your Django project's static files - amend as required
    location static/static-only {
        alias /home/django/django_project/static-only/; 
    }
    # Django static images
    location /static/django_project/images {
        alias /home/django/django_project/static-only/django_project/images/;
    }


    # Proxy the static assests for the Django Admin panel
    location /static/admin {
       alias /usr/lib/python2.7/dist-packages/django/contrib/admin/static/admin;
    }

    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass http://app_server;

    }
}

我在以下链接 http://nginx.org/en/docs/http/ngx_http_limit_req_module.html 上关注了文档

以下是“wget 127.0.0.1:9000”的结果

但不知道在哪里添加指令。好心提醒。

【问题讨论】:

  • 网络服务器是否收到请求?静态文件的url是否正常?
  • 当我打开我的主页时,它显示 504 Gateway Time-out nginx/1.4.6 (Ubuntu)
  • 你可以试试:wget 127.0.0.1:9000 看看web服务器是否运行良好
  • 我已经在主要问题中添加了快照......但它卡在等待响应......

标签: python django nginx


【解决方案1】:

如果你在 django 中使用 uwsgi,那么你可以在位置 place 的 nginx 配置文件中添加 uwsgi_read_timeout 指令

location / { 
    uwsgi_read_timeout 120; 
}

【讨论】:

    【解决方案2】:

    我在尝试更改 /etc/nginx/sites-available/django-project 时找到了解决方案。但是我需要在/etc/nginx/nginx.conf Nginx 的全局设置中添加以下行。我添加的行是:

    http {
        ...
        proxy_connect_timeout   10;
        proxy_send_timeout      15;
        proxy_read_timeout      20;
    }
    

    我托管了一个小型网站,上面的设置就足够了。但其他人可以根据自己的需要进行设置。

    【讨论】:

    • 这对我也有帮助。 Google 开发者工具向我显示了请求最长时间,我为这些参数设置的时间比最长时间多一点。
    【解决方案3】:

    我知道我在这里聚会迟到了,但在尝试了许多这些建议(和其他建议)后,我最终发现我的 DNS 发生了超时 - 如果您使用的是 Amazon 负载均衡器它们的“空闲超时”默认设置为 120 秒。

    【讨论】:

    • 你是如何解决这个问题的?我在我的 AWS 控制台中没有看到这个配置。我可以使用 .ebextensions 对其进行配置吗?
    • 控制台 -> EC2 -> 负载均衡器(左侧导航) -> 选择均衡器 -> 属性 -> 增加空闲超时