Apache vs Nginx – Clash of the Titans
- Ohad Hagai
- August 15, 2018
Apache and Nginx are the two most popular open source web servers that are powering the internet today. Both have their own unique characteristics. But which one is best for you? Let’s find out!
This debate is similar in many ways to the evergreen smartphone question – iOS or Android? Both are powerful mobile platforms with millions of users worldwide, but they operate very differently. While iPhones are less customizable, they arguably provide a richer user experience with better software support cycles. But where does the pendulum swing when it comes to Apache and Nginx?
What is Apache?
The Apache HTTP Server, often referred to as httpd or Apache, was first launched in 1995 and is being managed by the Apache Software Foundation since 1999. Apache powers 52% of all websites globally and is by far the most popular web server today. While mostly run on Linux, Apache can also be deployed on OS X and Windows. Apache is often chosen by administrators for its customization capabilities and widespread support.
Apache is the web server component of the popular LAMP (Linux, Apache, MySQL, PHP) stack, which is still standing strong despite the rise of NodeJS, rich clients JS frameworks, and other cloud services. Apache has a rich set of features that can be enabled by installing one of the 60 official modules. There are also many other unofficial modules that can be easily found on the internet.
What is Nginx?
Russian Igor Sysoev began developing Nginx back in 2002, with its first public release taking place in 2004. Nginx was developed as an answer to the so-called C10K Challenge (“How do you design a web server which can handle ten thousand concurrent connections?”). NGINX uses an asynchronous, event-driven architecture to handle these massive amounts of connections. This architecture makes handling high and fluctuating loads much more predictable in terms of RAM usage, CPU usage, and latency.
Nginx is second on the list of open source web servers by usage, powering around 30% of all websites. It uses the LEMP stack (Linux, “En-juhn-ex,” MySQL, PHP).
Apache vs Nginx – Let The Battle Begin
We’ll be comparing these two popular servers by examining their handling of static and dynamic content, memory usage, requests per second, configuration and security, and also compatibility and support. These parameters are vital when it comes to picking the right solution for your organization. Lets begin.
Static and Dynamic Content – Apache servers are fully capable of handling static content by using their conventional file-based methods.
Nginx does not possess the capability to process dynamic content natively. To handle PHP and other requests for dynamic content, Nginx must pass via an external processor for execution and wait for the rendered content to be sent back. The results can then be relayed to the client accordingly.
Apache’s ability to handle dynamic content internally means that configuration of dynamic processing tends to be simpler. Communication does not need to be coordinated with an additional (external/third party) piece of software and modules can easily be swapped out if the content requirements change.
Apache takes the lead. 1-0.
Memory Usage – Nginx comes out as clear leader in this aspect. Apache spawns more and more processes to handle new incoming requests, causing memory usage to grow fairly quickly. This can turn out to be a major issue if you have a big website with a lot of traffic.
In comparison to Apache, Nginx has fairly static memory usage and stays fairly stable regardless of the traffic volume.
Nginx equalizes. 1-1.
Requests per Second – This is essentially a measure of how fast the server can receive and serve requests at different levels of concurrency. The more requests they can handle per second, the more able the server is to handle large amounts of traffic. Nginx clearly dominates in the raw number of requests per second it can serve.
While tested locally at higher levels of concurrency (not absolute performance), it can handle fewer requests per second, but still more than Apache.
Nginx edges ahead. 2-1.
Configuration and Security – Apache includes an option to allow additional configuration on a per-directory basis by inspecting and interpreting directives in hidden files within the content directories themselves. These files are known as .htaccess files. This effectively allows decentralized configuration of the web server, which is often used for implementing URL rewrites, access restrictions, authorization and authentication, even caching policies.
Nginx does not interpret .htaccess files, nor does it provide any mechanism for evaluating per-directory configuration outside the main configuration file. While this presents an advantage from the security standpoint, keep in mind that there is a possibility to turn off .htaccess interpretation in Apache if needed.
We are on level terms again. 2-2.
Compatibility and Support – Apache’s ongoing popularity and usage has enabled it to get unprecedented levels of support. There is a large library of first and third party documentation available for the core server and for task-based scenarios involving Apache deployment. Many tools and web projects today include tools to bootstrap themselves within an Apache environment.
Nginx is not lagging far behind anymore, but its relatively smaller market share in the USA and Europe means fewer people have experience working with it. Also, the fact that the solution was created in Russia (with Russian documentation and support) means that its going to be a while before it achieves mainstream adoption in English speaking countries.
A photo finish win for Apache. 3-2.
Apache and Nginx: Can They Work Together?
Yes indeed! Apache and Nginx can also be friends.
A popular configuration for this partnership is to place Nginx in front of Apache as a reverse proxy. This allows Nginx to to handle all client requests, taking advantage of it’s fast processing speed and ability to handle large numbers of connections concurrently. This also allows you to scale out by adding additional backend servers as required.
Nginx is very good in processing and delivering static content. This advantage can be used for serving files quickly and directly to the client. For dynamic content (i.e – PHP files). Nginx can simply proxy the request to Apache, which can then process the results and return the rendered page to Nginx to pass the content back to the client.
In other words, Nginx’s speed can be leveraged to handle most requests, while forwarding the relevant ones to Apache. This can boost performance significantly.
And The Winner Is..
It must be mentioned that Apache has an impressive set of compatibility characteristics, which basically makes it a better out-of-the box solution. It’s probably the most flexible option you can find today. On the other hand, one can get serious performance/memory benefits by picking Nginx.
However, there is no clear cut winner in this duel. As demonstrated earlier in this article, both Apache and Nginx are capable solutions with their respective sets of inherited characteristics that can benefit various scenarios. There is also the option of incorporating the Node.js framework for added functionality.