Nginx – 504 Gateway Time-out uwsgi + nginx django application

djangonginxuwsgi

I'm trying to run my Django application using Nginx + uwsgi, but I receive 504 Gateway Time-out after one minute of loading.

My app takes time to do what needed as it searches for specific things on several websites.

My nginx conf is the next one:

upstream uwsgi {
    server 127.0.0.1:8000;
}

server {

    listen 80;
    server_name server_ip;

    root /opt/emails/subscriptions;
    index index.html index.htm index.php;

    location /emailsproject/ {
        root /opt/emails/subscriptions/;
    }

    location / {
        proxy_set_header X-Real-IP      $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://uwsgi;
        proxy_set_header Host $http_host;
        uwsgi_read_timeout 18000;
    }
}

My uwsgi script:

description "uWSGI server"

env PYTHONPATH=/opt/emails/subscriptions
env DJANGO_SETTINGS_MODULE=emailsproject.settings

start on runlevel [2345]
stop on runlevel [!2345]
respawn
exec uwsgi_python --http-socket  127.0.0.1:8000 -p 4 --wsgi-file /opt/emails/subscriptions/emailsproject/wsgi.py

My nginx is giving me the followin error message in error.log:

2015/09/28 02:15:57 [error] 4450#0: *19 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 37.235.53.246, server: my_server_ip, request: "POST /home/ HTTP/1.1", upstream: "http://127.0.0.1:8000/home/", host: "my_server_ip", referrer: "http://my_server_ip/home/"

Does anyone have any idea on how can I get rid of this ? I've tried the tons of stackoverflows solutions but none worked for me.

Best Answer

I know I'm late to the party but after trying many of these suggestions (and others) I eventually found the timeout for me was occurring from my DNS - if you're using Amazon load balancers they have an "Idle timeout" set at 120s default.