Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

504 Gateway Time-out uwsgi + nginx django application

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.

like image 945
Cajuu' Avatar asked Sep 28 '15 06:09

Cajuu'


People also ask

Can I fix 504 Gateway Timeout?

The 504 “Gateway Timeout” Error indicates that the browser sent an HTTP request to the server and it did not receive a response in time from another server to complete the request. In most cases, you can resolve it by refreshing the web page.

What causes a 504 Gateway Timeout?

A 504 Gateway Timeout Error means your web server didn't receive a timely response from another server upstream when it attempted to load one of your web pages. Put simply, your web servers aren't communicating with each other fast enough.


3 Answers

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.

like image 116
Ryu_hayabusa Avatar answered Nov 12 '22 20:11

Ryu_hayabusa


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.

like image 42
Zeno Avatar answered Nov 12 '22 20:11

Zeno


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;
        }
        ...
like image 1
elhoeste Avatar answered Nov 12 '22 18:11

elhoeste