Web servers are at the core of the internet. Without web server software such as Apache and NGINX, the web would be nothing like what we see today. However, they’re not interchangeable. To know which of those two options – Apache vs NGINX – is best for you, you need to understand how they work.
In this article, we’ll talk about what web servers are and how they work and introduce both Apache and NGINX. We’ll then compare Apache vs NGINX in terms of key differences and go over their pros and cons.
Let’s get to it!
What Is a Web Server?
In a nutshell, a web server is a computer dedicated to running server software such as Apache or NGINX. A web server both stores site files and can process incoming requests to access them.
It’s the web server software that’s responsible for this last task, which is an important responsibility. For popular websites, the software you use might need to handle massive numbers of requests in a small timeframe, so it has to be up to the task. Both NGINX and Apache are capable of scaling and handling large numbers of requests. However, the way that both server options work is different at a core level.
Web server software works alongside several other components to serve modern websites and applications. For example, to set up WordPress on a server you need the following components:
- Web server with HTTPS support: This can be Apache, NGINX, or a few other options.
- PHP: WordPress is built on the PHP language, so you need it to run the Content Management System (CMS).
- Database software: This can be MySQL or MariaDB.
For this article, we’re going to focus on web server software alone. However, it’s important to understand that software doesn’t work in a vacuum. In some cases, you can even configure web servers such as Apache and NGINX to work together. One way websites can do that is by configuring NGINX to work as a reverse proxy while using Apache as the main server software. The applications at your disposal is clearly varied and plentiful.
An Introduction to Apache
For a long time, Apache was the default web server option for most sites. The software has been around since 1995 when Robert McCool introduced the Apache HTTP Server. A few years later, the Apache Software Foundation took over maintenance and Apache became open-source software.
Apache garnered massive amounts of popularity because it came installed alongside several major Linux distributions. When it comes to web servers, Linux’s prevalence made Apache the de-facto choice for years.
The Apache name is so ingrained with Linux that the Operating System (OS) uses the httpd command for its server process (something found within Apache itself). However, Apache isn’t the only big name when it comes to web servers.
An Introduction to NGINX
NGINX was designed by Igor Sysoev to be able to handle what is known as the “C10K problem”. This deals with optimizing a network to be able to handle up to 10K concurrent connections. In other words, NGINX was created with maximum scalability in mind.
The open-source software was released in 2004, and now there are two license types available for it. You have the open-source version and NGINX Plus, which is an enterprise solution that includes professional support and other features.
In its infancy, most websites used NGINX to handle only static requests due to its perceived installation difficulty versus Apache. NGINX’s market share has grown steadily throughout the years though.
According to some surveys, NGINX has passed Apache as the most popular web server software in the world. Other numbers put it right on Apache’s heels:
What is true is that NGINX is much more popular than Apache among the most popular websites on the web. This is because NGINX has an undeniable edge when it comes to scalability, which is key for sites that get massive amounts of traffic.
Also, NGINX can pull “double duty” as a reverse proxy or load balancer, if you don’t want to use it as a web server. In other words, it can fulfill a lot more roles than Apache can.
However, this isn’t to say that NGINX should be your default choice if you’re considering changing servers. Throughout the next few sections, we’ll break down what makes each software unique.
Apache vs NGINX: 3 Key Differences
Before we can talk about which server solution is the best option for you, we need to break down what makes them different. Let’s start by talking about request handling regarding Apache vs NGINX.
1. Request Management
Be warned – this is where things get a bit more technical. Apache uses what it calls Multi-Processing Modules (MPMs) to handle connections. The software enables you to choose between three types of MPMs:
- mpm-prefork: This module creates single-threaded processes to handle each request. Each “child”, in turn, can only handle a single connection.
- mpm_worker: This module can launch multi-threaded processes, with each thread being capable of managing one connection.
- mpm_event: For the most part, this module works the same as mpm_worker, but is optimized to keep connections alive.
One unique aspect of Apache is that it enables you to choose which module you want to use, and swap them as you need. By default, Apache uses the mpm-prefork module, which works great for low numbers of concurrent connections. The mpm_worker module is better when it comes to scaling, but it requires additional server resources.
In contrast, NGINX uses what it calls an “event-driven” approach. The software uses multi-threaded processes, with each thread being capable of handling multiple connections.
What’s more, NGINX deals with incoming requests asynchronously. In a nutshell, this means NGINX can manage more requests with fewer server resources, making it more efficient in this particular aspect.
2. Server Configuration
Apache uses .htaccess files to set specific rules for how it handles incoming requests. Those files include everything from redirection rules to memory limits, and more.
Using Apache, you can set up individual .htaccess files for every directory in your server tree. To give you an example, if you set up a shared server you can enable each website hosted on it to set up their own .htaccess files.
The downside to this high level of flexibility is that every .htaccess file you run into along a request path needs to be interpreted separately. This means requests can take longer to execute.
NGINX doesn’t provide you the same level of flexibility when it comes to configuration. Instead of directly-level configuration files, you have one main file that governs the entire thing.
Modules are add-ons that you can use alongside your server software of choice to extend its default functionality. Considering Apache’s age and popularity, it shouldn’t come as a surprise that it has the edge over NGINX when it comes to module selection.
Not only does Apache offer more modules, but it tends to be easier to find documentation and tutorials for how to use them. Moreover, Apache enables you to install, enable, and disable modules at will, providing you with a high degree of flexibility.
NGINX doesn’t offer the same advantage. To use modules, you need to compile them into NGINX core. Moreover, you can’t disable modules once they’re compiled, making it much more difficult to swap options.
It’s important to note that NGINX Plus does include a dynamic module feature that enables you to disable and enable modules at will. However, even with Plus, you still need to compile those modules with NGINX core before you can use them.
Although NGINX’s module library and documentation are growing at a steady pace, they still can’t match Apache in terms of volume. Considering how NGINX keeps gaining more market share, though, this probably will change in the near future.
The Pros and Cons of Apache vs NGINX
By now, you understand the core differences between Apache and NGINX. Let’s now give a brief breakdown of each software’s pros and cons.
To start with, let’s talk about the main benefits of using Apache:
- It can be easier to set up and configure
- You get more granular control over your server’s configuration thanks to .htaccess files
- The module selection is better and you can enable and disable modules at will
- You can decide how to handle requests using different modules
The primary downside of using Apache vs NGINX is that the latter simply scales better. If you’re still in the growing stage of your website, Apache should be more than enough to handle the traffic.
However, at some point you might need to consider updating your server stack if your site becomes dramatically successful. This could mean switching to NGINX or using it as a reverse proxy for your Apache web server.
NGINX has two clear advantages when it comes to Apache – performance and scalability. To be more specific, NGINX is better when it comes to:
- Handling massive numbers of concurrent requests
- Getting better performance with fewer hardware resources
- Serving static content faster
This last part is why so many people choose to use NGINX as a reverse proxy solution. Despite all its performance-related benefits though, NGINX is not without flaws.
Although having one single configuration file translates to decreased request times, it also means NGINX is less flexible than Apache. This applies to modules as well, since the open-source version of NGINX forces you to compile modules to use them – and you can’t disable them. In practice, this means setting up NGINX to work as you need can be much more complex than using Apache.
The primary reason to use NGINX is if performance is your primary concern. At some point, popular websites need to pull out the big guns to handle massive traffic without downtimes, long loading times, and more. Also, since NGINX can enable you to get better performance results with fewer hardware resources, it can be a more cost-effective option.
Switching servers or server software is always a big decision. As such, you’ll ideally want to use the server solution that you’re going to stick with for the long term.
Choosing between Apache vs NGINX is not as clear-cut as it might seem. In many cases, the decision won’t be up to you since your web host will decide what software to use. On a practical level, you’ll need to do your research and find out which option a web host uses before committing to them.
Do you have any questions about how to choose between Apache and NGINX? Let’s talk about them in the comments section below!
Article thumbnail image by ProStockStudio / shutterstock.com