Nginx vs Apache: Head to Head Comparison & How to Choose

Both Nginx and Apache are mature products with rich feature sets and high performance. They share common open-source origins, and you can deploy them either on Windows or Linux servers. 

However, some key differences might make you choose one over the other.

For example, Apache is a comprehensive solution that supports many different technologies and modules out-of-the-box. In contrast, Nginx relies on third-party modules to expand its functionality.

To see which web server might better suit your needs, let's dive deeper into these options.

Apache – The Original Hero Web Server

Apache

Created in 1995 by Robert McCool and originally called the “Apache HTTP Server Project” (hence the name), Apache was designed to create a robust, commercial-grade server that is free to use, even with modifications. It became popular very quickly because it could run on many different operating systems, from Unix to Windows.

Because of this long-running popularity, I consider Apache as the “original hero” web server. It's robust, well documented, and supported by an open community of developers under the auspices of the Apache Software Foundation.

Nginx – Titan of a New Era

Nginx

Nginx (pronounced as “Engine X”) is an HTTP and reverse proxy server, a mail proxy server, and a generic TCP/UDP proxy server used to host websites and applications of all sizes. It was first publicly released by Russian developer Igor Sysoev. Nginx's initial goal was to solve the C10K problem that Apache struggled to manage. 

Also Read

Notable Differences Between Nginx & Apache

Architecture

Nginx and Apache share some similarities in their core architecture. For example, they both use master-worker processes to improve performance. They even have similar configuration files. Yet the differences in architectural style result in significant broad-view performance variation.

Nginx has a resource-friendly event-driven architecture that uses small but constant amounts of memory under load. This characteristic makes it ideal for hosting websites with high traffic levels or those that have intermittent traffic spikes.

Apache's process-driven architecture handles each connection via a dedicated thread, which requires more memory. However, it scales better under heavy loads on machines with more CPU cores and RAM.

Memory Usage

Nginx is known for its high performance and low resource consumption. On the other hand, Apache can be memory intensive, especially when running multiple server blocks. While both use memory to handle HTTP requests, Nginx is more lightweight. 

The design of Apache meant that it spawned one thread per connection, and each thread would use a certain amount of RAM. As traffic increased, this could lead to problems as more RAM would be required, particularly on servers with less memory. Apache also creates new processes for each request, even from the same user.

Comparatively, Nginx uses one process to handle multiple connections at once. 

PHP Handling

Because both of these web servers mainly work with PHP, how they handle the code means significant performance potential. Nginx does not execute PHP directly by default. Instead, it passes the request to PHP-FPM (FastCGI Process Manager), which handles the request and sends a response back to Nginx, which then serves the content back to the client.

Since Nginx does not wait for a response from PHP-FPM to serve another request (similarly to how it does not wait for a response from clients when serving static content), Nginx can handle more requests concurrently than Apache will be able to manage.

Apache uses a module called mod_php to execute PHP code. In this model, every time an HTTP request comes in, Apache spawns a new process or thread (depending on how it's configured) to handle that request. This process is also responsible for handling any PHP requests within that request.

This model works, but it has some drawbacks. For one thing, spawning a new process for every request can be intensive on the system, especially if there are many simultaneous requests. Generating a new process for every PHP request within a request is even more intensive since the operating system has to spawn a brand new interpreter for each one.

How to Choose Between Nginx and Apache

As you can see, there's no clear winner between these web server behemoths. It mainly depends on what you need the webserver to manage. 

Choose Apache if: 

  • You are running a medium/large scale website that is likely to grow significantly over time and requires custom modules.
  • You have many virtual hosts or modules enabled in your server and need them all on startup.
  • You're running a small website and don't want to spend the time learning how to configure Nginx properly.

Use Nginx if:

  • You are running a large-scale website and want to easily configure caching & load balancing at the same time without worrying about hardware limitations.

Final Thoughts on Nginx vs Apache

Whether you run Apache or Nginx will depend on your needs and the hardware you're running. You can use either option for serving PHP websites. But there is a lot more to consider than just that. 

If you have a simple website, you may not notice any difference between them. But if your site gets more traffic and grows, you'll need to know how each server performs and scales under load.

Also Read

Photo of author

Article by Timothy Shim