It’s very common for people to ask which is better between Apache and Nginx. Both servers are great, but this constant doubt made us think about this post. Our intention, here, is to make a comparison that clarifies the choice for our readers.
We’ll start answering two simple questions:
What is Apache?
The Apache web server appeared in 1995 and has been under the direction of the Apache Software Foundation since 1999. It is the most popular web server since 1996. Administrators often choose it for its flexibility and power. Its strong point: It can interpret a large number of languages without going through external software.
Apache is the web server component of the very popular LAMP stack (Linux, Apache, MySQL, PHP). Although there are many other components of stack (for example, NodeJS, rich client JS frameworks, etc.), LAMP remains very popular.
The Apache web server has a full set of features that can be activated by installing one of the approximately 60 official modules or one of many other unofficial existing modules.
Over the years, Apache has developed several methods for handling web requests to improve its efficiency (mainly RAM utilization and latency). You can configure the Apache request processing method in three ways. I will break down the three main multiprocess modules (MPMs) below:
- process model: this is the initial “pre-fork” method; It does not improve as the number of simultaneous connections increases. That is because it consumes a lot of RAM and could even refuse connections at high loads. Smaller sites will not notice it, but larger sites will probably do it.
- worker model: This template creates a unique control process that is responsible for launching child processes. Each child process then creates a fixed number of threads, as well as a listening thread. The listener’s thread listens to the connections and forwards them to a thread for processing upon their arrival.
- event model: It is similar to the worker model, but it creates a listening thread that listens for connections and passes them to a worker thread for processing. This MPM handles long connections much more efficiently on a single thread. Since Apache 2.4, the event model has been deemed stable and is now the default setting.
What is Nginx?
Nginx, exists since 2002. It was designed to manage a large number of simultaneous connections. While Apache is limited to 256 simultaneous connections, Nginx can handle more than 10,000. Its popularity has been that it consumes little memory and is very efficient at handling static content. It is most often used to manage static content and relay dynamic content to another service, such as Apache or Lighttpd.
NGINX was created in response to the C10K challenge to manage at least 10,000 simultaneous client connections on a single server. NGINX uses an asynchronous and event architecture to handle these huge amounts of connections. This architecture makes the management of high and fluctuating loads much more predictable in terms of RAM utilization, CPU utilization, and latency.
The main difference between NGINX and Apache, in terms of event patterns, is that NGINX does not configure additional work processes per connection. In most cases, the recommended NGINX configuration runs a CPU-based worker process, which maximizes hardware efficiency. NGINX also has a full set of features and can perform different server roles:
- A reverse proxy server for HTTP, HTTPS, SMTP, POP3, and IMAP
- A load balancer and an HTTP cache
- A frontend proxy for Apache and other web servers, for example the combination of Apache’s flexibility and NGINX’s good static content performance
NGINX supports FastCGI and SCGI Managers for dynamic content scripting service such as PHP and Python. It uses the LEMP stack: a variant of LAMP using the phonetic spelling of NGINX (Linux, “En-juhn-ex”, MySQL, PHP).
Apache or Nginx — What’s the best option?
Let’s now compare the two web servers in different aspects. We will find that Apache and NGINX are worthy competitors, but depending on your use case, one may suit better to a project than the other.
Below, we compare the performance of Apache with that of NGINX both in static content deserts, and in that of dynamic content.
Static Site: NGINX is about 2.5 times faster than Apache based on the results of a benchmark test running up to 1000 simultaneous connections. Another test running with 512 simultaneous connections showed that NGINX is about twice as fast and consumed a little less memory (4% less). Obviously, NGINX serves static content much faster than Apache. If you need a lot of static content at high levels of competition, NGINX is more efficient.
Dynamic Sites: A benchmark test made in 2015 comparing the dynamic content desertification by Apache and NGINX revealed that apache’s mpm_event, associated with the PHP-FPM module, can handle the same number of PHP requests as NGINX. The reason is that almost all of the processing time is spent in the PHP runtime environment rather than the central part of the web server. The PHP runtime environment is quite similar between the two web servers.
In terms of PHP (and probably other languages too), the performance of servers to deliver dynamic pages is virtually the same. If you really want to speed up the loading of dynamic pages, you can choose from several options. For example, add cached applications such as Varnish, switch to a faster PHP execution time, load balancing, or add hardware.
Support for the operating system
Apache runs on all kinds of Unix-like systems (for example, Linux or BSD) and has full support for Microsoft Windows. NGINX also works with several modern Unix-like systems and has support for Windows, but its Windows performance is not as strong as other platforms. Apache shows more advantage on this point.
Both projects have an excellent safety record for their C code base. The NGINX code base, however, is considerably smaller by several orders of magnitude. This is certainly a big advantage from a security point of view. There are vulnerability reports available for Apache 2.2 and 2.4. NGINX also has a list of recent security advisories.
Nginx and Apache are extensible across module systems, but the way they work differs considerably.
The Apache module system allows you to handle modules dynamically to meet your needs during server operation. The Apache kernel is always present, while modules can be enabled or disabled by adding or removing additional features and retaining the primary server. Apache uses this feature for a wide variety of tasks.
Due to the maturity of the platform, there is an extensive library of modules available. You can use them to modify some of the main features of the server. For example mod_php, which integrates a PHP interpreter into every worker running. However, the modules are not limited to the processing of dynamic content. Among other functions, you can use them to rewrite URLs, authenticate clients, enhance server security, save, cache, compress, limit transfer rates and encrypt. Dynamic modules can greatly extend the basic functionality without much extra work.
Nginx also implements a module system, but it is quite different from the Apache system. In Nginx, modules do not load dynamically, so you must select and compile them into the base software.
For many users, this will make Nginx much less flexible. This is especially true for users who are not comfortable maintaining their own compiled software outside the classic management system of their distribution. While distribution packages tend to include the most commonly used modules, if you need a non-standard module, you will have to create the server from the sources yourself.
However, Nginx modules are still very useful and they allow you to dictate what you want to your server by including only the features you want to use. Some users may also consider this to be more secure because arbitrary components can not be loaded on the server. Nginx modules offer as many possibilities as on Apache. For example, Nginx modules can provide support, compression, rate limiting, logging, rewrite, geolocation, authentication, encryption, broadcast, and messaging functions.
Support for the Apache community is through mailing lists, IRC, and Stack deployment. Apache sales support is available from a number of third-party companies, such as OpenLogic, but the Apache Foundation does not maintain any official lists.
NGINX has community support via mailing lists, IRC, Stack Overflow and a forum. The company behind NGINX offers a commercial product called NGINX Plus, which supports a set of additional features regarding load balancing, media transfer and tracking.
Conclusion – So, Apache or Nginx?
Both options, in their latest versions, can compete with each other in most areas. For static content, NGINX is a king, but for dynamic content, the difference in performance is small. NGINX shines with some of its more advanced features (multimedia streaming, reverse proxy for non-HTTP protocols), as well as its commercial support and training.
Owners of high traffic websites that need to serve a lot of static content and/or media streams will probably prefer NGINX (or use a combination of Apache and NGINX). In most other website use cases, both Apache and Nginx will do the job very well
While you don’t make up your mind, check this post about cloud computing!