Simon Willison’s Weblog

Subscribe

What are the advantages of running Apache behind nginx as opposed to just Apache by itself?

12th October 2010

My answer to What are the advantages of running Apache behind nginx as opposed to just Apache by itself? on Quora

I do this for all of my Django stuff—I have Django running on modwsgi on a stripped down Apache (almost no configuration except for the modwsgi stuff), then I put an nginx on port 80 which serves the static files directly and proxies dynamic requests back to Apache.

There are a few advantages to this. Firstly, I use non-threaded Apache so each Apache worker process tends to be pretty big (having a full Python interpreter + Django application running in it). It’s a horrible waste of resources for one of those fat processes to sit around serving up a static file instead of serving a dynamic request. Secondly, nginx deals with slow connections for me—if someone on a modem (or 3G phone) hits my site I don’t tie up an Apache process for several seconds to respond to their request. Instead, nginx sucks the response in from Apache in a few ms and then spools it out to the slow client at whatever rate they can handle. Since nginx is evented it can handle hundreds or even thousands of slow clients at once using very little memory.

Finally, if I’m running an nginx proxy at the “front” of my stack it’s trivial for me to start load balancing to more than one Apache servers if I need to.