Django Nginx Gunicorn = 504 Timeout

django, gunicorn, nginx, python

Solution

When you hold down F5:

- You've started hundreds of requests.

- Those requests have filled your gunicorn request queue.

- The request handlers have not been culled as soon as the connection drops.

- Your latest requests are stuck in the queue behind all the previous requests.

- Nginx times out.

- For everyone.

Solutions:

- Set up rate-limiting buckets in Nginx, keyed on IP, such that one malicious user can't spam you with requests and DOS your site.

- Set up a global rate-limiting bucket in Nginx such that you don't overfill your request queue.

- Make Nginx serve a nice "Reddit is under heavy load" style page, so users know that this is a purposeful event

Or:

Replace gunicorn with uwsgi. It's faster, more memory efficient, integrates smoothly with nginx, and most importantly: It will kill the request handler immediately if the connection drops, such that F5 spam can't kill your server.

Problem

I'm trying to publish a Django application on the production server using Nginx + Gunicorn. When I doing a simple stress test on the server (holding the F5 key for a minute) the server returns a `504 Gateway Time-out` error. Why does this happen? This error only appears for the user when doing multiple concurrent requests, or the system will be fully unavailable to everyone?

Original source