Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nginx and uWSGI gives "504 Gateway timeout"

I am following the tutorial Setting up Django and your web server with uWSGI and nginx.

uWSGI is up and running

I set up uwsgi to serve my Django project with the following line.

mydjangoproj $ uwsgi --http 0.0.0.0:8002 --module wsgi --harakiri 5

This works when I go there in a browser, to 42.42.42.42:8002.

My nginx setup

nginx is running as a daemon, and visiting it's default site, port 80, works.

I added this as a site to nginx using the following mydjangoproj_nginx.conf file:

server {
    listen      8000;
    server_name 42.42.42.42;
    charset     utf-8;
    client_max_body_size 75M;

    location /static {
        alias /home/myuser/mydjangoproj/static; 
    }

    location / {
        uwsgi_pass 127.0.0.1:8002;
        include     /home/myuser/mydjangoproj/uwsgi_params;
    }
}

I use the unmodified version of uwsgi_params, from the tutorial:

uwsgi_param QUERY_STRING $query_string;
uwsgi_param REQUEST_METHOD $request_method;
uwsgi_param CONTENT_TYPE $content_type;
uwsgi_param CONTENT_LENGTH $content_length;
uwsgi_param REQUEST_URI $request_uri;
uwsgi_param PATH_INFO $document_uri;
uwsgi_param DOCUMENT_ROOT $document_root;
uwsgi_param SERVER_PROTOCOL $server_protocol;
uwsgi_param HTTPS $https if_not_empty;
uwsgi_param REMOTE_ADDR $remote_addr;
uwsgi_param REMOTE_PORT $remote_port;
uwsgi_param SERVER_PORT $server_port;
uwsgi_param SERVER_NAME $server_name;

It does serve the static files perfectly.

Error

If I visit 42.42.42.42:8000 it hangs for a long time, until the nginx timeout I guess, and I get 504 Gateway Time-out.

uWSGI writes nothing in the shell. If visiting directly in browser, it does write about receiving a request.

The nginx error log writes, only after the timeout:

2014/12/11 05:31:12 [error] 28895#0: *1 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 66.66.66.66, server: 42.42.42.42, request: "GET / HTTP/1.1", upstream: "uwsgi://42.42.42.42:8002", host: "42.42.42.42:8000"

If I close the uWSGI, which is just run from a shell, I instantly get a 502 Bad Gateway.

When searching online, people just recommend setting the uWSGI timeout lower than the nginx timeout, that's why I run uWSGI with --harakiri 5.

So, what is my problem here?

like image 381
Mads Skjern Avatar asked Dec 11 '14 10:12

Mads Skjern


1 Answers

I think you are running uwsgi in http mode --http 0.0.0.0:8002 but you have configured nginx as wsgi proxy change your uwsgi script as:

 uwsgi --socket :8002 --module wsgi --harakiri 5

Note that if you are running nginx and uwsgi on the same machine is better to use unix sockets

like image 88
sax Avatar answered Nov 04 '22 00:11

sax