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

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

我在将我的Django项目托管在Amazon EC2上时遇到了问题。
使用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,通常会出现一个问题,即长请求被挂起并最终超时,但是我不确定如何将其应用于我的项目,因为我只是在尝试加载网站。

不确定如何调试此问题,因此不胜感激!

最佳答案

这可以帮助您:

我所做的是,我编辑了/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文件中做任何更改

关于django - 使用Django提供静态文件时,HTTP 504网关超时(Nginx + Gunicorn),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40602120/

相关文章:

nginx - 我可以使用 vanilla Nginx 将请求 header 值作为响应正文回显吗?

django - 如何设置运行Django的AWS Elastic Beanstalk Docker平台?

django - 如何通过 nginx 提供 django 媒体文件?

python - 使用 Django-Tagging 时 URL 中的未知说明符

nginx - Nginx多个服务器 block 监听相同的端口

ruby-on-rails-4 - Rails/Nginx/Capistrano/Puma : (111: Connection refused) while connecting to upstream

django - Docker - psql : django. db.utils.OperationalError: FATAL: 角色 "admin"不存在

docker - 为Docker运行镜像没有看到结果

python - Django:如何引发 Http401 和 Http403 异常,如 Http404,RAISE EXCEPTION not RESPONSE

docker - 在 Dockerfile 中运行 sqlcmd