Search code examples
nginxwebgunicornportforwarding

Why is NGINX giving 504 gateway timeout error?


My NGINX conf is-

server {
    listen 80;
    server_name site.com ;

    location / {
        include proxy_params;
        proxy_pass http://0.0.0.0:8000;

    }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/site.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/site.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}

I am running a Flask app on Gunicorn on Port 8000. When I try to access my site, I get a 504 Gateway Time-out nginx/1.14.0 (Ubuntu).

In the error.log , it says-

 upstream timed out (110: Connection timed out) while connecting to upstream, client:myip server: site.com, request: "GET / HTTP/1.1", upstream: "http://0.0.0.0:8000/", host: "mysite.com"


Solution

  • You have some errors in your config.

    • You cannot have one server who is listening on multiple ports.
    • You should not write 0.0.0.0:8000, but try 127.0.0.1:8000 instead

    Also I dont understand why you include proxy params, just delete that line.

    Here is an example of what you could do.

    server {
            server_name site.com www.site.com;
            location / {
                    proxy_pass http://127.0.0.1:8000/;
            }
            listen [::]:443 ssl; # managed by Certbot
            listen 443 ssl; # managed by Certbot
            ssl_certificate /etc/letsencrypt/live/site.com/fullchain.pem;
            ssl_certificate_key /etc/letsencrypt/live/site.com/privkey.pem;
            include /etc/letsencrypt/options-ssl-nginx.conf;
            ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
    }
    server {
            if ($host = www.site.com) {
                    return 301 https://$host$request_uri;
            }
            if ($host = site.com) {
                    return 301 https://$host$request_uri;
            }
            listen 80;
            listen [::]:80;
            server_name site.com www.site.com;
            return 404;
    }
    

    Good Luck :-)