Apache vs Nginx: which is the better web server

June 18, 2020

When it comes to serving web pages to visitors on the internet, few server programs come close to Apache and Nginx, the two most popular web servers out there. 70% of the market consists of just these two open-source competitors and while they perform the same tasks, they are not completely interchangeable. Each has certain advantages when it comes to specific workloads and content, so the end choice usually depends on these factors. For a long time, Apache dominated the market, but with Nginx’s growing popularity these two are tied in a rough stalemate. To see which web server prevails in the Apache vs Nginx debate, first, we must take a quick look at their background and characteristics.

 

Apache

Created in 1995 by Robert McCool, this web server has been the most used server since 1996 and is the component of the widely popular LAMP stack. Maintained by the Apache Foundation, it’s well documented and supported, offering around 60 official modules and many unofficial ones to add and enhance its features.

Throughout the years the people behind Apache had to come up with new ways to handle web requests. Thanks to these changes, this web server uses improved processing methodology and became an overall well-rounded program. It has three main Multi-Processing Modules:

  • Mpm_preforke;
  • Mpm_worker;
  • Mpm_event.

With these, you can choose and handle different dynamic connections.

Pre-installed Linux distributions

One of the things that lead to Apache’s quick rise is the fact that it comes with pre-installed major Linux distributions, like Ubuntu, CentOS, Red Hat, etc. Linux OS is probably the most used operating system when it comes to VPS hosting in general. So, a web server that supports and comes pre-packaged with OS distros is definitely going to dominate the market.

.htaccess

Another thing you can notice while diving deeper into the Apache vs Nginx debate is that Apache uses .htaccess while Nginx doesn’t support it. And since it’s been around for so long, there are many tutorials about how to work with this file. Essentially, you can customize how it handles incoming requests and other processes such as:

  • URL rewrites;
  • redirection rules;
  • memory limits;
  • maximum upload file sizes;
  • cookies;
  • directory protection;
  • query string manipulations;
  • encoding and cache-control headers.

Furthermore, users can create and configure separate .htaccess files for every level and directory in the directory tree. This means that you can change specific details in contained environments without modifying the global server configurations, an especially beneficial feature for shared hosting environments.

The downside to this is that for each single resource request, to load them Apache has to go through the entire directory until it gets to the server’s root directory. After processing the files, it has to reconfigure itself for each directory, and after that PHP still has to execute the appropriate stacks and MySQL has to assemble the web page. Because of this, the performance takes a hit.

Dynamic module system

Another thing that makes this web server stand out in the Apache vs Nginx question is the modules. While both have these, Apache is a bit different as it allows users to install them right after the web server finishes installing and enable or disable them. You can do that with two simple commands (a2enmod & a2dismod) without having to edit configuration files.

 

Nginx

This web server was first introduced in 2004 by the Russian developer Igor Sysoev and it was created to address Apache’s performance limitations. There are two versions you can choose from: Nginx Plus, which has additional enterprise features, and the standard open-source with a BSD-type license.

At first, it was used mainly as a load-balancer, reverse proxy, or to serve static files along with Apache. But as it evolved, users started to replace Apache with this web server. It can handle thousands of connections simultaneously and doesn’t require a lot of resources as it doesn’t set up extra processes for each connection.

Configuration

Nginx doesn’t employ a configuration system, naturally making it faster and more efficient. Because there are no directory-level configurations, each request can be handled more quickly, improving the web server’s overall performance. On the other hand, it’s not very favourable when it comes to shared hosting environments. That is why users working with Windows and Linux VPS usually prefer Nginx.

Static content

Another aspect that makes it stand out in the Apache vs Nginx debate is that Nginx can’t process dynamic content natively. If it does get requests for it, the web server passes it to an external processor and only gets back the rendered content which can cause significant delays when loading big websites with loads of dynamic content. On the other hand, it does support dynamic content handlers such as FastCGI and SCGI.

 

How they differ

Caching

Basically, caching allows you to reuse previously retrieved data by storing it in a high-speed data storage layer. This allows for faster website loading times as the server doesn’t have to process each repeated web request to the same browser for the same page as it’s already prepared.

Because Nginx is highly efficient when serving static content, it doesn’t really need caching for this. But because many users still employ this server along with dynamic content, it does have FastCGI caching which comes with a standard installation just for this.

Apache has the standard mod_cache module which can become glitchy if it conflicts with other modules. It also has the Varnish HTTP accelerator, which works a bit better than the module.

Handling requests

The biggest difference in the Apache vs Nginx fight is the way both server handle requests. As previously mentioned, Apache uses Multi-Processing Modules. They bind to network ports on the machine, accept requests, and dispatch worker processes to handle requests.

The oldest and previously used MPM was efficient as it spawned new processes with one thread on every request, embedding the PHP interpreter in every single process. The mpm_worker and mpm_event modules fixed this by switching to php-fpm.

Nginx’s worker processes, on the other hand, can handle thousands of incoming connections per worker, meaning that no new threads and processes are created for each request. That is why in general, this web server is better suited to handle loads of requests without affecting the overall performance.

Static and dynamic content

Apache can handle static content via its conventional file-based methods. It also processes and serves dynamic content though its internal dynamic processor without having to send it to external components.

While Nginx excels in serving static content, it can’t natively process dynamic content. This communication has to be configured and additional handlers have to be enabled to do so.

In the end, the right answer to the Apache vs Nginx question boils down to user preference. Both are great options but for different purposes. The former is better for serving dynamic content and allows for per-directory configuration. The latter is more suited for static websites and heavy workloads. If you really want to, you can even use both by placing Nginx as a reverse proxy in front of Apache.