Apache vs Nginx Servers: The Lowdown On These Two Server Types
The internet runs on a collection of servers. However, these have a different setup than a standard desktop machine. A savvy sysadmin or back-end developer will optimize this choice to the needs they have. The primary way to do this is through a choice of server software. One popular comparison in this area is Apache vs Nginx.
This is for a number of reasons, but usage numbers and longevity are two major ones. Nginx is newer and arguably more capable for modern web standards. However, Apache doesn’t pull any punches either, and is just as capable of serving websites to millions of users worldwide.
In this post, we’re going to give you a comparison of Apache vs Nginx. We’re going to look at surface-level elements such as the popularity of both server types. However, we’ll also dig into the technical features and considerations for each too. By the end, you’ll know the ins and outs of both software types, and which one is best for your server.
What Apache and Nginx Are
Reading this article means you likely understand that a web server requires specialist software in order to provide optimal performance. While this piece won’t cover what server software is or does in a general sense, we will cover two of the leading solutions for your server:
- Apache. This is free and open-source software that comes pre-installed with most Linux ‘distros’. It’s been around since 1995, and because of these two factors, has a near-leading position in the market.
- Nginx. While it’s a relative baby in comparison to other server software, Nginx has lots of users. At current writing, it’s almost joint-first with Apache for the most popular server software (more of which later).
It’s worth noting that Apache has been leading server software for decades, and has seen a large market share for that time. However, Nginx is now the front-runner, although it’s not by much.
CAPTION: The blue line here from google Trends represents searches for Apache since 2004, while the red line relates to Nginx.
As for what Apache and Nginx bring to the table, this could fill several books on the subject. However, we’re going to try to do this is in under 2,000 words – starting with the general feature set of both.
The General Features of Apache and Nginx
Because Apache and Nginx are both HTTP web server software, you’ll find a lot of similarities. For example, they are both open-source, which means they will work on a variety of systems. By extension, both are flexible and extendable, but there are different goals for each server type.
However, Apache vs Nginx is not a true comparison, in that both pieces of software approaches the process of configuring a web server in a different way. In reality, both will suit different applications better.
Over the rest of this article, we’ll show you this through a breakdown of the technical details for Apache and Nginx. Here’s what we’re going to cover:
- A technical breakdown of each server software type, that includes request and connection handling, caching technologies, and more.
- Configuring and extending Apache, including details on its
.htaccess
file and modules. - How you configure Nginx, and its extensibility.
This doesn’t seem a lot to consider, but you can unpack each one into a number of other sub-section. We’ll do exactly this, starting with the technical differences.
The Technical Differences Between Apache and Nginx
In a general sense, Apache and Nginx take almost unique and exclusive paths to achieve the same goal of an optimal web server. If you compare each one in a broad way, you’ll see how different they are:
- Apache uses a ‘process-driven’ architecture, while Nginx uses an ‘event-driven’ one.
- While both types server static files (albeit in a different way), Nginx doesn’t server dynamic content.
- Each software type interprets HTTP requests in a different core way.
We’re going to look at some of these aspects, starting with connection handling considerations.
Connection Handling
You’ll find that Apache vs Nginx places a lot of importance on how each type handles connections and requests. For example, Apache uses a process-driven approach, where it will create a new request for each thread using a processing module called mpm_prefork
. This can be fast if the number of requests are fewer than the number of processes, but there’s a performance hit after this.
In fact, Apache offers lots of flexibility for creating a connection handling algorithm to suit your needs using mpm_prefork
, mpm_worker
, and mpm_event
modules. In contrast, the design of Nginx is such that it tries to mitigate the negative performance aspects Apache has.
It does this through the way it spawns worker processes on an event-driven basis. This means Nginx is great for scaling, as it can manage multiple requests within a single thread.
Request Interpretation
Both Apache and Nginx also take a different approach to interpreting the requests a server will receive. For example, Nginx uses the Uniform Resource Identifier (URI) of a request to translate it to a physical element of the filesystem.
You can see this in the format of some of the configuration blocks, such as server
and location
. They work together to parse a URI and turn it into something within the filesystem.
server {
listen 80;
server_name example.org www.example.org;
root /data/www;
location / {
index index.html index.php;
}
location ~* \.(gif|jpg|png)$ {
expires 30d;
}
location ~ \.php$ {
fastcgi_pass localhost:9000;
fastcgi_param SCRIPT_FILENAME
$document_root$fastcgi_script_name;
include fastcgi_params;
}
}
In contrast, Apache’s roots as a ‘pure’ web server software means it assumes and acts on the basis that every request is a resource in the filesystem. However, Apache can also work with URIs where the request is more abstract. Despite this, the documentation warns against using anything other than the file-based method.
Static vs Dynamic Performance
When it comes to performance, Apache vs Nginx is an important consideration for almost every user. In context, the comparison boils down to how each software type serves static and dynamic content.
Because Apache’s inherent architecture works with the filesystem in a direct way, it’s great at serving static content. However, it will also process dynamic content within the server, which is a simile implementation that is also flexible if you need to swap out modules based on the requirements you have.
In comparison, Nginx doesn’t process any dynamic content at all. It’s more complicated, because Nginx has to pass off these requests to an external worker or protocol. Even so, you’ll find there is less of a performance overhead, and Nginx can utilize other protocols with greater adaptability.
For static content, this can be just as straightforward as Apache, and in some cases a better performing option. This is because Nginx can contact the interpreter when it needs to, rather than it be an inherent part of the server software’s overall performance.
Apache’s Distributed Configuration Model
It will be no surprise that Apache and Nginx both have a different approach to its server configuration. In short, the configuration of Apache vs Nginx rely on whether you can do so at a directory level.
Apache uses a distributed model, which means you can configure the server on a per-directory basis. You’ll do this through .htaccess
files. Apache will check each component of the requested file path for a .htaccess
file, and carry out the directives inside.
In our opinion, a .htaccess
file is much more flexible than any other system. It’s quick to find, open, and edit. The power at your fingertips to perform lots of powerful actions is grand. You’ll also find that an Apache server will process a .htaccess
file whenever it discovers one, without the need to reload the server.
It’s also fantastic if you need to let some users control aspects of their sites within impacting the entire server configuration. This is why Apache servers are a mainstay of shared hosting platforms, along with its flexible module system.
Apache’s Modules
You’re able to load and unload modules on a dynamic basis using Apache. This is akin to using plugins and themes within WordPress: The core Apache functionality carries out the fundamental steps, while the active modules help you to create a unique experience.
These modules can cover a lot of ground, and there are many available. Modules such as mod_php
can change the core functionality of the server, while the popular mod_rewrite
lets you carry out many other tasks:
- Restricting IP addresses.
- Changing the home folder for your site.
- Carrying out 301 redirects.
- Setting a default home page.
Between .htaccess
and the dynamic module system, Apache represents a clear way for most users to tinker and optimize a web server and site.
Nginx’s Centralized Configuration Model
Nginx’s takes a centralized approach to server configuration. This means there is one configuration file (ending with a .conf
extension). It’s sometimes difficult to find depending on your server structure.
This has some clear disadvantages for a budding site owner that wants to make changes to a server. For sites on shared hosting, it’s almost impossible to configure the server. There is also much less flexibility.
However, there are also a few advantages, based around performance. Because Nginx only has one location for a configuration file, and Apache could potentially use multiple locations, there will only be one lookup. By extension, there will less of a performance hit to locate that configuration file too.
You’ll also find that because the configuration is central, site owners won’t have responsibility to manage security-related aspects. This is another boon for Nginx’s approach, because the security responsibility lies on the shoulders of the system administrator only.
Nginx’s Modules
It’s fair to say that Nginx’s module system is nowhere near as permissive or flexible as Apache, despite offering the same sort of functionality. For example, take Nginx’s redirect rules:
server {
listen 80;
server_name www.olddomain.com;
return 301 $scheme://www.newdomain.com$request_uri;
}
This is because you can’t load them as dynamic elements with Nginx. To go back to our WordPress analogy, this is like having to reinstall your entire site to deactivate a plugin.
On the surface, this doesn’t seem workable, and appears to be a massive detriment. However, what you lack in flexibility, you gain in core stability and functionality. If you know what your server needs at build-time, you can include all of those component and modules.
There’s a security aspect to consider too. Because you won’t swap dynamic modules in and out of your system, you’ll know what every element does and can do. It makes your server’s structure and architecture clear and easier to maintain, as well as more performant.
Why You’d Use Either Apache or Nginx
To sum up, Apache vs Nginx is not a clear-cut decision. For a start, the typical opinion of Nginx being a better performer than Apache is misleading. If you set up an Apache server well, it’s just as performant as an equivalent Nginx server.
Apache is great for serving both static and dynamic content in a simple way. It offers immense power for those new to managing a server, flexibility in configuration. In a general sense, you’ll need less technical knowledge to configure an Apache server than Nginx.
However, performance is where Nginx shines. The setup of its architecture makes sure that it can achieve more per processor cycle than an Apache server. This is despite less flexibility in set up.
In some cases, you may not get the choice of Apache vs Nginx, especially if you use a shared host. However, if you do, the choice will depend on your expertise, goals, and needs.
Summary
Your choice of server software matters, and you have myriad options at your disposal. In reality, you’re not going to choose a server type from left-field. Instead, you’ll often select from two or three types. Apache vs Nginx is a oft-considered comparison, because both offer standout, long-term performance and reliability.
This article has looked at how Apache vs Nginx compares on these two fronts and more. There are a whole host of technical considerations to pore over, and this will factor into your choice more than how popular a particular server software is among the community.
Do you have an opinion on Apache vs Nginx? Share your thoughts with us in the comments section below!
Leave a Reply