Nginx and Apache together earn 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 lighter than Apache. This is a problem that is rooted in design – under the hood, Apache must create a new process thread for each connection. And although it can process 10 threads at a comparable speed to Nginx, when scaled up to hundreds of simultaneous connections, Nginx takes a decisive lead.
Nginx works differently and can process multiple connections within a single process thread. It is designed to also act as a simple reverse proxy, so rather than include the overhead of a full web server, it is simply designed to move data bytes from one site to another. Nginx can be over twice as fast as Apache when serving static content and much less CPU-intensive when doing so, making it great for running on low-powered systems.
However, this speed only matters when delivering static content to many users at once. When you start serving dynamic content, the bottleneck lies elsewhere, for example 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 (including Apache) specifically to host static content. A useful example is Node applications on the server side, where HTTP requests to dynamic pages must be forwarded to a server running Express, but static resources (images, stylesheets, etc.) can be served from Nginx. The cost of doing this is minimal and can accelerate 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 it easy to run multiple websites on the same server. For example, you can add a VirtualHost block like this
DocumentRoot /var/www/html ServerName www.firstsite.com
This allows Apache to route all requests
www.firstsite.com to a specific folder, which can have its own settings independent of other sites running on the server. This can be supplemented further with
.htaccess files, which can overwrite the server configuration of a specific folder. But run websites with
.htaccess files are slower than using global config, and are only recommended in cases where you want to allow other users to change the server settings of 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 its own folders, but it’s just not that configurable, especially with a lack of similar functionality
.htaccess. But if you really need that kind of functionality, you can use Nginx as a reverse proxy to access content served elsewhere, even run on another instance by Nginx or multiple Docker containers.
Apache also has much better plug-in support, in the form of modules that can be loaded dynamically 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 are both decent web servers. They are both free and open source, although Nginx has a paid version called Nginx Plus that adds software load balancing on top of Nginx with open source. Both servers are secure, have good support groups and are easy to configure. While Nginx is gaining in speed, both are comparable in terms of dynamic content.
If you are not sure which one to use, go with Nginx. In general, if you are running a Unix system, Nginx is a good choice simply because of its speed and ease of use. If you need more configuration, especially if you run many websites from the same server, Apache can work better.
One thing to note is that if you use PHP, the setting for Nginx is a little different. Apache runs PHP built into the same process and communicates directly with the PHP engine. Nginx runs it in a separate process (
PHP-FPM) and communicates with it as a reverse proxy. Both methods are equally fast, but your configuration varies.
Nginx and Apache both support all Unix systems, including FreeBSD. While Nginx technically has a version running on Windows, it is not the best. Apache is fully supported on Windows, and as such is the go-to web server on that platform.