504 error when the view loading takes more than 5

2020-02-07 10:37发布

问题:

I've got an installation with django-nginx-gunicorn-supervisor-postgresql. In the Django admin, I've got a 504 if the loading of the view takes more than around 5 seconds.

For instance, if I filter a change list view with many records and takes more than that time, the 504 appears. The same view with fewer records works, as long as it takes less time.

I've noticed also that some views are still running in the background, even after the 504, cause I can see the modifications they make in the database after they finish.

I've tried to increase all timeouts I've found (nginx, gunicorn), but none of them was configured to 5 seconds. Any guess of what could be misconfigured? Or where could be the timeout that raises the 504?

Thanks

The timeouts I've configured are:

In /etc/nginx/sites-enabled/mysite

proxy_connect_timeout 60;
proxy_read_timeout 90;
proxy_send_timeout 90;
send_timeout 90;
fastcgi_read_timeout 300;

In /etc/nginx/nginx.conf

send_timeout 300;
keepalive_timeout 65;
proxy_connect_timeout 300;
proxy_read_timeout 300;
proxy_send_timeout 300;

In the gunicorn_start file (launched by supervisor)

exec gunicorn -c ${CONF_FILE} ${DJANGO_WSGI_MODULE}:application \
        --name ${NAME} \
        --user=${USER} --group=${GROUP} \
        --log-level=debug \
        --timeout=90

In the gunicorn.conf file (previous ${CONF_FILE})

timeout = 90
graceful_timeout = 30
keepalive = 3

回答1:

I've found the best way to debug things is to start at Django and work your way up till you figure out what is timing out.

First, check that Django isn't timing out.

In one terminal run:

python manage.py runserver 127.0.0.1:8000

And then in a terminal on the same machine and do:

wget http://127.0.0.1:8000/<path_to_your_admin_view>

If that works then check to see if Gunicorn is timing out:

In the Gunicorn config file, change the settings so that it's binding to a local port instead of a socket:

exec gunicorn -c ${CONF_FILE} ${DJANGO_WSGI_MODULE}:application \
        --name ${NAME} \
        --user=${USER} --group=${GROUP} \
        --log-level=debug \
        --bind=127.0.0.1:8001
        --timeout=90

Make sure you restart supervisor. Then in a terminal on the same machine do:

 wget http://127.0.0.1:8001/<path_to_your_admin_view>

If that works then undo the change to the gunicorn config, restart supervisor and connect directly to nginx to see if it is timing out:

 wget http://127.0.0.1:80/<path_to_your_admin_view>

If this works your problem is likely upstream in a proxy or load balancer.