Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Nginx Gunicorn = 504 Timeout

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?

like image 653
Pedro Fernandes Avatar asked Nov 29 '13 02:11

Pedro Fernandes


1 Answers

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.

like image 106
Thomas Avatar answered Sep 19 '22 11:09

Thomas