12

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?

Mads Skjern
  • 5,648
  • 6
  • 36
  • 40

1 Answers1

26

I think you are running uwsgi in http mode --http 0.0.0.0:8002 but you have configured nginx as uwsgi proxy. You should change your uwsgi script to something like:

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

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

Justin Hammond
  • 595
  • 8
  • 20
sax
  • 3,708
  • 19
  • 22
  • That did it! I thought the communication between nginx and uwsgi should be http, but apparently is is a different kind of connection. – Mads Skjern Dec 11 '14 at 14:08
  • I normally wait a few days for other answers to pop up, and then pick the best one. I don't know if this is against guidelines. But I've marked it answered now :) – Mads Skjern Dec 11 '14 at 16:14
  • how is --harakiri 5 different from --socket-timeout 5 ? – DJ_Stuffy_K May 05 '21 at 17:40
  • https://stackoverflow.com/questions/67407446/oserror-write-error-python-flask-sigpipe-writing-to-a-closed-pipe-socket-fd – DJ_Stuffy_K May 05 '21 at 20:12