504 Gateway Time-out uwsgi + nginx django application

16,509

Solution 1

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.

Solution 2

Many people assume that 504 is related to request from client or if there are some DDoS attack on site with huge session pile up. 504 works both ways as you could see the error if your wsgi is not able to present code and/or if server is not able to respond due to overflow. So make sure you run manage.py to double check if your code is presentable. Then try "curl -I yoursite.com" command to see if its giving you error still. There are two nginx related file you need to focus on one is in /etc/nginx/nginx.conf for default global settings. Second one is what you create your self in /etc/nginx/sites-available. in your global /etc/nginx/nginx.conf installation add following lines

proxy_connect_timeout   10;
proxy_send_timeout      15;
proxy_read_timeout      20;

This solution addresses 90% of 504 error caused in different scenarios. Your project conf should be same as you've mentioned above.

Solution 3

That's because you need to set proxy_read_timeout, not uwsgi_read_timeout. And that, in its turn, it's because you are actually not using uwsgi, you are using HTTP proxying. Uwsgi backends are declared using uwsgi_pass directive.

Share:
16,509

Related videos on Youtube

Cajuu'
Author by

Cajuu'

Python and C enthusiast.

Updated on September 18, 2022

Comments

  • Cajuu'
    Cajuu' over 1 year

    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.

  • Cajuu'
    Cajuu' over 8 years
    That apparently got me to another error: Message: no such session (Driver info: chromedriver=2.9.248304,platform=Linux 3.13.0-57-generic x86_64). I don't receive this message on my localhost
  • drookie
    drookie over 8 years
    This has nothing to do with nginx, this is some sort of application error on a server.