When setting up a web server, a commonly asked question among web developers / owners is whether they should choose Nginx vs Apache. There is no right or wrong answer to this. Both web servers are amongst the most actively used open source servers on the Internet. According to Netcraft, as of July 2016, 68% of all active websites were either using Apache or Nginx.
The web server you choose can be largely dependent upon personal preference as well as what works best for the project at hand. This article will discuss the differences that exist between both popular web servers to help you get a better understanding of where each one excels.
Overview of Nginx
Nginx is a free and open-source web server created back in 2002 in response to the C10K challenge which set out to handle 10,000+ concurrent connections on a single server. Nginx used, and still uses, an asynchronous event-driven architecture in order to handle these large amounts of connections. A detailed diagram of how Nginx works is shown below.
Img src: aosabook.org
One main different from Nginx vs Apache is that Nginx does not set up a new process for each web request but rather the default configuration is to have one worker process per CPU. Nginx can also be used in various of scenarios including:
- It can act as a reverse proxy server for TCP, UDP, HTTP, HTTPS, SMTP, POP3, and IMAP protocols
- It can act as a load balancer and an HTTP cache
Overview of Apache
Apache is currently at the “top of the food chain” when is comes to web servers. With currently almost half of all active sites using Apache as their web server (as of July 2016) it holds the largest market share. Apache development began in 1995 and since 1999 has been developed under the Apache Software Foundation.
The Apache web server has progressed vastly since its inception. It offers users a vast list of both official and unofficial modules to enhance the server’s feature set and perform various tasks. Apache is also flexible in terms of how it processes web requests. This is dependent on the Multi-Processing Module (MPM) used. The tree main Apache MPMs are:
Pros and Cons of Nginx vs Apache
Now that we have a brief introduction to the history / functionality of each web server, we’ll go over a few of the pros and cons for each in the lists below.
|More lightweight – requires fewer resources / memory||Less community support|
|Able to handle 10k+ simultaneous connections with a low memory footprint||Less extensive list of modules|
|Better at handling static content|
|Recommended for sites running on a VPS|
|Greater selection of modules||Consumes more RAM under heavier load|
|Better suited for shared hosting||Spawns new processes for each request making things less efficient|
|More widely used / larger community|
|Allows for configuration of .htaccess file|
When to Use Nginx vs Apache
There are a few factors which come into play when deciding whether you should use an Nginx or Apache web server. The pros and cons section above can also help determine which web server is best suited for your use-case. If you want improved performance in regards to static content then the better web server is in most cases, Nginx. Additionally, Nginx also comes ahead in terms of scalability, media streaming, VPS hosting, and reverse proxying.
On the other hand, Apache may be the preferred web server in other scenarios. Many users may want to modify an .htaccess file for server configuration purposes which is not available in Nginx. Additionally, Apache is better suited for shared hosting environments and can provide a control panel making it easier to make server-side changes. Apache also has a larger community / user base providing beginners with more readily available information and support.
Using Nginx and Apache Together
If you can’t choose between Nginx vs Apache, there is also the option to use a combination of both. Nginx and Apache can both be used as proxy servers. However, the most common approach is to put Nginx in front of Apache as a reverse proxy. With this setup, Nginx will handle all client requests. If the request is for static content, then Nginx will deliver said content. Alternatively, if the request is for dynamic content, Nginx will proxy the request onto Apache where it will be processed and returned back to Nginx in order to be passed along to the client.
This combination of Nginx and Apache is appealing to certain website owners who want to offload some of the request processing requirements from Apache and allow Nginx to serve static content, as it does best. It is greatly dependent upon your web server requirements which will determine whether you employ an Nginx vs Apache web server or use a combination of both.