使用Django(Nginx + Gunicorn)提供静态文件时HTTP 504网关超时

时间:2016-11-15 04:36:47

标签: django nginx docker gunicorn http-status-code-504

我在Amazon EC2上托管我的Django项目时遇到了问题。 使用Gunicorn和Nginx来托管网站时,我在浏览器中尝试加载页面时出现以下错误(摘自Javascript控制台):

Failed to load resource: the server responded with a status of 504 (Gateway Time-out): https://example.com/favicon.ico 

我相信Nginx在找到我的静态文件时遇到了一些问题,但我不确定原因。这是我的Nginx配置:

server {
    listen 443 default;
    client_max_body_size 100M;
    server_name www.example.com;

    keepalive_timeout 5;

    ssl on;
    ssl_certificate /etc/letsencrypt/live/www.example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/www.example.com/privkey.pem;

    # the domain name it will serve for
    charset     utf-8;

    # path for static files
    root /opt/app/staticfiles;

    location /static {
        root /opt/app/staticfiles;
    }

    location / {
        # checks for static file, if not found proxy to app
        try_files $uri @proxy_to_app;
    }

    location @proxy_to_app {
        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;
    }

}

/var/log/nginx/access.log和cat /var/log/nginx/error.log不显示任何内容。

对于HTTP代码504,通常存在一个问题,即长请求挂起并最终超时,但我不确定它是如何应用于我的项目的,因为我只是尝试加载网站

不确定如何调试此问题,敬请任何帮助!

1 个答案:

答案 0 :(得分:0)

这可能会对您有所帮助:

我所做的是我编辑了/etc/nginx/site-enabled/default文件如下:

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

        root /usr/share/nginx/html;
        index index.html index.htm;

        # Make site accessible from http://localhost/
#       server_name localhost;
        # provide whole path of static files you got after running `python manage.py collectstatics`
        location /static{
                 alias /home/ubuntu/folder1/folder2/webpage/static;
        }

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                proxy_pass http://0.0.0.0:8134;#Provide your django server's link
                # Uncomment to enable naxsi on this location
                # include /etc/nginx/naxsi.rules
        }
}

注意: - 确保您已完成python manage.py collectstatics。不要在nginx.conf文件中进行任何更改