简体   繁体   中英

HTTP 504 Gateway Time-out when serving static file with Django (Nginx + Gunicorn)

I've been having problem hosting my Django project on Amazon EC2. Using Gunicorn and Nginx to host the site, I get the following error when trying to load my page in the browser (excerpt from the Javascript console):

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

I believe Nginx has some problems finding my static files, but I'm not sure why. Here's my Nginx config:

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 and cat /var/log/nginx/error.log don't show anything.

For HTTP code 504 it is usually a problem that a long request is hanging and eventually times out, but I'm not sure how it applies to my project, since I'm only trying to load the site.

Not sure how to debug this issue, so any help is appreciated!

This may help you :

What I did is I have edited /etc/nginx/site-enabled/default file as follows:

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
        }
}

Note :- Make sure you have done python manage.py collectstatics . Don't do any changes in nginx.conf file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM