Can anyone explain why putting up nginx and reverse proxying Apache is faster than just plain Apache?

I've already moved all my static files to s3 (I heard nginx is better at handling static files), so what other benefits does it have?


Apache has two models for handling connections from clients: worker and prefork. Prefork is very resource heavy (requires a full process for every client connection) but still commonly used with PHP. Worker is much better from a resource point of view, but still requires a dedicated thread per client connection.

Nginx uses only a small number of threads (commonly only one thread per cpu) to handle all client connections. This makes it lighter than Apache and allows it to scale up and handle tens of thousands of connections on a single machine.

However, this doesn't make it "better" than Apache for all cases. It generally makes it better than Apache for serving up static files, but not dynamic content generated from a web application (i.e. you don't want hundreds of connections being blocked while one of the requests is doing a database query). This is why you still need fastcgi, uWSGI, passenger, or even apache+mod_wsgi+passenger to offload web app work into the app server. These back-end processes are still going to use one thread per request, but at least it's only for the requests that need dynamic content.

So the main reasons nginx + apache is faster than straight apache is (A) static files handled much more efficiently, (B) reduced load on (heavy) apache resources.

Finally, for the "other benefits" part of your question: nginx also handles SSL connections faster than Apache although again, that doesn't make it better for all cases. For example, if you need detailed authentication using client-side certificates you'll still need Apache to handle the SSL. If you're just running pages over HTTPS for the encryption then nginx will give you a little speed boost.


Simply , Apache is excellent for dynamic content processing but you have to wait it's response.

The reversed proxy (putting Nginx between user and Apache) saves you time cause apache is responding to which is on the same machine as if you are accessing localhost.

So Apache client will always be Nginx and the response from apache is a static content which will make you get the best of Nginx as it is faster than apache in serving this content.

You can checkout this diagram

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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