views:

58

answers:

1

This question is not nginx vs apache. I am more interested in the architectural advantages of NGinx over Apache. As I was able to understand -

  • nginx is an asynchronous, event-driven, web-server which outperforms Apache by a huge margin.

Why is this? Where does Apache fall behind?

+2  A: 

There is no single reason why nginx strictly "outperforms" Apache. For many load patterns you may configure Apache so that it handles this load. For some (very busy) load patterns nginx in default configuration can exhibit performance degradations, and can require fine tuning to work right.

However, it has been the experience of many, that nginx actually works "better" out of the box, or with simple tuning. Many systems' performance clearly improved when nginx was installed as a front-end, with Apache moved to back end.

The primary reason is that nginx is event-driven, and contains the state machine which handles the lifecycle of connections. That way, you can have very few "worker" processes, each handling many hundreds or even thousands of connections simultaneously. For Apache you will have to run the same number of child processes (or threads) as the number of connections.

It is obvious that three processes against a thousand processes should be a huge win, at very least.

In particular, nginx easily allows to greatly reduce the load of serving static files (images, Javascript, CSS). Handling each additional connection in nginx is very cheap, so as the static files are usually a majority in terms of number of requests, you get efficient processing.

Also, nginx performance is better for "slow clients". When you have Apache looking straight to the Internet, and clients send requests over (congested) lines, your (fast) server will have to patiently feed the (slow) client, waiting until it consumes the entire response. Thus the Apache child (or thread) cannot do anything useful. Nginx worker, on the other hand, simply keeps this slow connection in epoll set of descriptors, all the while processing other connections.

From the conceptual point of view, you should always try to separate the "classes" of requests, with their own performance profile and demands. E.g., serving small static files is one of such classes; serving dynamic pages is another such class; serving huge static files is yet another. Introducing nginx to your system implicitly handles this separation.

squadette