Django Nginx Gunicorn = 504 Timeout

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.