Apache vs. Nginx: Which Web Server Is Better Performance?
Nginx and Apache combined serve over 50% of the web. But Apache has seen a decline in recent years, in favor of Nginx. Is Nginx really better, and are there any benefits to using Apache?
Nginx Is Newer and Faster
Nginx is much more lightweight than Apache. This is a problem rooted in design under the hood, Apache must create a new process thread for every connection. And while it may process 10 threads at a comparable speed to Nginx, when it’s scaled up to hundreds of concurrent connections Nginx takes a deciding lead.
Nginx works differently, and can process multiple connections within a single process thread. It’s designed to also function as a simple reverse proxy, so rather than bring the overhead of a full web server it’s simply designed to move bytes of data from one place to another. Nginx can be over twice as fast as Apache when serving static content, and much less CPU intensive when doing so, which makes it excellent for running on low powered systems.
However, this speed only really matters when serving static content to a lot of users at once. When you start serving dynamic content, the bottleneck lies elsewhere, such as in your PHP engine, your web app, or your database.
Because Nginx is also a reverse proxy, you can use it in front of another server (even Apache) specifically to host static content. A useful example is server-side rendering Node applications, where HTTP requests to dynamic pages need to be forwarded to a server running Express, but static resources (images, stylesheets, etc.) can be served from Nginx. The overhead when doing this is minimal, and can speed up static content significantly.
Apache Is More Configurable
Apache is more focused on being a web server, and has some useful features like directory-based configuration files and virtual hosts. This makes running multiple sites on the same server very easy. For example, you can add a VirtualHost block like this to
DocumentRoot /var/www/html ServerName www.onlyinfotech.com
This will make Apache route all requests to
www.onlyinfotech.com to a specific folder, which can have its own settings independent of other sites running on the server. This can be further augmented with
.htaccess files, which can overwrite the server config for a specific folder. However, running sites with
.htaccess files is slower than using global config, and is only recommended in cases where you want to allow other users to modify the server settings for a directory without touching the global config (shared hosting is the main example of this).
Nginx can also serve multiple domains from a single server, and route them to their own folders, but it’s just not as configurable, especially with a lack of functionality similar to
.htaccess. But, if you really need that kind of functionality, you could use Nginx as a reverse proxy to access content served elsewhere, even running on another instance of Nginx or multiple Docker containers.
Apache also has much better plugin support, in the form of modules that can be dynamically loaded without restarting the server. Nginx also supports modules, but only recently added module loading, so many modules still require you to patch the binary.
Which Server Should You Choose?
Despite the shortcomings, they’re both decent web servers. They’re both free and open source, though Nginx does have a paid version called Nginx Plus that adds a software load balancer on top of open-source Nginx. Both servers are secure, have good support communities, and are easy to configure. While Nginx wins in speed, both are comparable at serving dynamic content.
If you’re unsure which to use, go with Nginx. In general, if you’re running a Unix system, Nginx is a good choice simply due to its speed and ease of use. If you need more configuration, especially if you’re running many websites off the same server, Apache may work better.
One thing to note is that if you’re making use of PHP, the set up for Nginx is a bit different. Apache natively runs PHP natively in the same process, communicating directly with the PHP engine. Nginx runs it in a separate process (
PHP-FPM) and communicates with it like a reverse proxy. Both methods are just as fast, but your configuration will vary.
Nginx and Apache both fully support any Unix system, including FreeBSD. While Nginx technically has a version that runs on Windows, it’s not the best. Apache is fully supported on Windows, and as such is the go-to web server on that platform.