A Comparison Between Popular Web Servers
According to a census held in January 2012, Apache servers hold 64.9% of the market share, the highest in the category of web servers used to power a website, even though it is not advisable to overlay high traffic websites on Apache web servers. The third most popular web server according to the same survey is Nginx which holds only 9.63% of the total websites. Though its market share is quite modest, it is giving a tuff competition to Apache in terms of traffic handling and its load balancing capabilities.
Even some famous websites like Wordspress.com & pinterest.com are believed to deploy Nginx as a web server for their portals instead of Apache. The reason of such belief is the resource optimization, high traffic handling and load balancing capabilities of Nginx, which Apache surely lacks.
Nginx, publicly released in 2004, is an open source, free, http, reverse proxy as well as IMAP/POP3 proxy server. Nginx, pronounced as “Engine-x”, is known for its
- low consumption of resources
- High traffic handling and performance
Nginx is capable of handling up to 10,000 requests per second with the usage of minimum server resources. In contrast to the process based model of Apache which exhausts under high traffic condition, Nginx overlays its working criteria on a non-blocking event based model. While using a php website with Apache server:
- Initially user makes a request to access the website so Apache server, embedded with php binary, allocates a process for the request.
- Since php binary is embedded, it supports the fast communication between the process and php website but it also consumes about 20-50 MB of RAM per process.
- Another point to be noted is that Apache cannot handle multiple requests simultaneously, so it creates a child process for each parent process to handle multiple requests, which again consumes 20-50 MB of RAM for each sub process.
- From the above point we can conclude the problem in high traffic conditions because memory resources at server will exhaust rapidly, causing the server to use virtual memory which further slows down the processing speed.
- Moreover, until the connection is not terminated, each process remains tied to the request in case there is any kind of delay in execution due to slow internet connection, processing load etc. This causes hundreds of processes to remain pending creating a delay and in high traffic conditions, server will ultimately crash.
On the other hand, Nginx server creates an event for subsequent requests made by the user. This means that,
- Initially when a user requests the access, a process is created for the user and when further requests are generated from the same user, an event is triggered. These events are monitored and controlled by the process either in parallel or in sequential manner, as the scheduling of events is drafted.
- This saves a large amount of un-necessary memory consumption by small or static processes was the problem with Apache server.
- However, php binary cannot be embedded in Nginx server so to overcome this problem ‘php with FastCGI’ or ‘Apache+mod_php’ process is created in background to handle php requests.
- So, whenever a static request is made from user, it is handled by Nginx and all php requests are bypassed to the backend process. When a request is processed, it responds to the user. During this time, Nginx handles other events and on receiving the reply from php process, it allocates another request to this backend process. This optimizes the different resources for maximum productivity.
- Nginx can also be used as a load balancer under extremely high traffic conditions. It distributes the traffic request to other backend servers sustaining the amount of processing time for every request. This is called as the “reverse proxy” mode.
So even if a website’s requirement is to deploy an Apache server, Nginx can still be deployed to handle high traffic and using the reverse proxy mode, Apache requests can be distributed among various Apache server in backend. This makes Nginx a universal and high performance server for every type of website and traffic handling requirement.