504 Gateway Time-out uwsgi + nginx django application

13,330

Solution 1

If its an internal task that takes too much time for processing, use celery to run the task. http://docs.celeryproject.org/en/latest/userguide/tasks.html

If its not purely an internal task, eg: - uploading a large file, then increase the Nginx client_body_timeout to greater than 60s.

Its because of the default timeout in nginx config. Edit the Nginx virtual host file and add the following line in server{} section. http://nginx.org/en/docs/http/ngx_http_core_module.html#client_body_timeout

# default is 60 seconds, For a 300 second timeout. 
client_body_timeout 300s;

Edit: uwsgi_read_timeout 300s; is also needed. But its already in your config.

Solution 2

Late to the party. I spent hours configuring nginx.cfg and ended up realizing it's related to the load balancer setting of my EC2 server. If you are using AWS Load Balancers for your EC2 server, try to also increase the Idle timeout along with the change of nginx.cfg.

Solution 3

My application environment is Django/Python/Nginx and it is not on AWS. I found many clues where they indicated the modification of these parameters and I applied them, but none worked for me until I tried to modify a file called "default" found in /etc/nginx/sites-available/ and added the line: uwsgi_read_timeout 3600;

I made this modification within the location area of "listen 443", looking like this:

server { 
        listen 443 ...
        ...
        location / {
                   uwsgi_pass django;
                   ...
                   uwsgi_read_timeout 3600;
        }
        ...
Share:
13,330
Cajuu'
Author by

Cajuu'

Python and C enthusiast.

Updated on July 23, 2022

Comments

  • Cajuu'
    Cajuu' almost 2 years

    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.