Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a Django site running on Gunicorn with a reverse proxy through Nginx. Isn't Nginx just an extra unnecessary overhead? How does adding that on top of Gunicorn help?

share|improve this question

1 Answer 1

up vote 16 down vote accepted

I'm going to focus on slow client behavior, and how your configuration handles it, but don't be tempted to believe that is the only benefit. The same method that benefits slow clients also has benefits for fast clients, SSL handling, dealing with traffic surges, and other aspects of serving HTTP on the Internet.

Gunicorn is pre-forking software. For low latency communications, such as load balancer to app server or communications between services, pre-fork systems can be very successful. There is no cost in spinning up a process to handle the request, and a single process can be dedicated to handling a single request; the elimination of these things can lead to an overall faster, more efficient system until the number of simultaneous connections exceeds the number of available processes to handle them.

In your situation, you are dealing with high latency clients over the internet. These slow clients can tie up those same processes. When QPS matters, the application code needs to receive, handle, and resolve the request as quickly as possible so it can move on to another request. When slow clients communicate directly with your system, they tie up that process and slow it down. Instead of handling and disposing of the request as quickly as possible, that process now also has to wait around for the slow client. Effective QPS goes down.

Handling large numbers of connections with very little cpu and memory cost is what asynchronous servers like Nginx are good at. They aren't affected in the same negative manner by slow clients because they are adept at handling large numbers of clients simultaneously. In Nginx's case, running on modern hardware it can handle tens of thousands of connections at once.

Nginx in front of a pre-forking server is a great combination. Nginx handles communications with clients, and doesn't suffer a penalty for handling slow clients. It sends requests to the backend as fast as the backend can handle those requests, enabling the backend to be as efficient with server resources as possible. The backend returns the result as soon as it calculates it, and Nginx buffers that response to feed it to slow clients at their own pace. Meanwhile, the backend can move on to handling another request even as the slow client is still receiving the result.

share|improve this answer
    
nitpicking: gunicorn is a preforking event-based server. each process handles several connections asynchronously. That doesn't change the fact that nginx have a way lower cost per connection, making the rest of your explanation valid. –  Javier Jan 8 '11 at 12:45
1  
And as a bonus, Nginx stops "Slow HTTP DoS Attacks" very effectively. The one point I would add is that when using Nginx it can mess any up any IP related stuff you might be doing in Gunicorn, for example auth by ip, logging etc. unless Gunicorn supports looking at the X-Forwarded-For header. I'm not familiar with Gunicorn so I can't say how big an issue this will be for you. –  Niall Donegan Jan 8 '11 at 12:48
    
Thanks for the info Javier, I'm not more than passingly familiar with gunicorn. –  blueben Jan 8 '11 at 18:58
    
Your answer should fit for this question: stackoverflow.com/q/13182892/719276 –  arthur.sw Dec 4 at 10:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.