Apache vs. NGINX: Which Web Server Is Better?
In this blog, our expert compares Apache vs. NGINX to help teams decide which web server is a better fit for their needs. The Apache HTTP Server (often referred to as just “Apache”) and NGINX are often at the top of the list for enterprises considering open source web servers. However, there are some important differences between them that teams should understand before deciding which one to implement.
Keep reading to learn about the different features and use cases for Apache vs. NGINX, two popular web infrastructure technologies.
What Is the Apache HTTP Server?
The Apache HTTP Server is a free open source web server created by a group of software developers that formed the Apache Group.
The Apache HTTP Server started as an offshoot of the NCSA httpd server in 1995, and has expanded since then. The Apache Software Foundation was formed in 1999 to support the Apache HTTP Server.
Back to topWhat Is NGINX?
NGINX is a free open source HTTP and reverse proxy server, along with a generic TCP/UDP proxy server. NGINX Plus is the commercial offering, which is available through paid subscription and includes additional features.
NGINX was created in 2004, and a company was created in 2011 to provide support and the NGINX Plus paid software, which was then acquired by F5 in 2019.
Back to topApache vs. NGINX: Key Differences
The main difference between Apache vs. NGINX is that Apache is better for shared hosting environments and serving dynamic content, where as NGINX performs best with static files and is often used as a front-end or proxy to back-end services.
Apache and NGINX both have distinct strengths and limitations. In this section, we’ll look at how their features differ across several key areas.
Feature | Apache | NGINX |
Architecture | Modular/multi-threaded | Event-driven |
Static Content | Caching modules add caching to base server | Built-in caching system |
Dynamic Content | Integrates language processor into modules | Passes requests to an external process that provides content (proxying to backend application) |
Configuration | Can include files in order to separate common configuration options or features and supports virtual servers and configurations for separate locations or paths; also allows directory-level configuration using .htaccess files | Can include files in order to separate common configuration options or features and supports virtual servers and configurations for separate locations or paths; also supports updating the binary without losing connections |
Client Requests | Event, worker, or prefork processing models | Event-driven model (can process multiple requests simultaneously) |
Flexibility | More than 50 dynamically loadable modules to add additional functionality or features; third-party modules also available | More than 100 third-party modules, but they are not dynamically loaded, so they need to be compiled within the core software itself; dynamically loaded modules are available with NGINX Plus |
Support | Documentation, users list, IRC chat channel, or through a third-party | Documentation, IRC channel; NGINX Plus comes with support |
Apache vs. NGINX: Architecture
Apache is currently built with a modular architecture. The core has the direct interaction with the client, but modules are used to add extra features from the base. Nearly everything else is a module, even SSL/TLS. There are many modules for different features, such as URL rewriting and load balancing. Apache has several different worker models — the most popular are prefork, worker, and event, and each has its own advantages and disadvantages.
NGINX uses an event-driven architecture which is similar to Apache, but with one worker per cpu. NGINX is able to upgrade the binary without downtime using the same method as reloading a new config. It does this by starting a new main process to handle new connections, with old workers ending when their connections are completed until all old processes have exited.
Apache vs. NGINX: Static and Dynamic Content
Both Apache and NGINX can serve static files directly from the file system. NGINX has a built-in caching system that is designed to be efficient and scalable. It was originally designed to be a reverse proxy. Tests have shown it outperforming Apache in serving static files by 2–3 times. Apache, however, relies on caching modules to add caching to the base server.
They also can both serve dynamic content, but by different methods. Apache can provide dynamic content by integrating a language processor in a module, and providing it directly. NGINX, however, needs to pass requests to an external process that provides content, which Apache can do as well. This means NGINX is really just proxying to a backend application, which is where its strength lies.
Apache vs. NGINX: Configuration
Both Apache and NGINX have text configuration files that are similar in nature, but not identical. They both can include files in order to separate common configuration options or features. They both support virtual servers and configurations for separate locations or paths.
However, Apache allows directory-level configuration using .htaccess files, while NGINX doesn't. This makes NGINX a little more efficient since it doesn't need to search for .htaccess files and interpret them, and a bit more secure by not allowing additional configuration. However, this makes Apache a bit more appropriate for shared environments since users of individual sites can apply their own configurations.
They both support reloading the configuration without restarting and dropping connections, but they do it in different ways. NGINX also supports updating the binary without losing connections.
Apache vs. NGINX: Client Requests
In both Apache and NGINX, the main process listens for connections and passes them to worker processes after accepting one. Then the workers process the request based on their model — event-driven for NGINX, and either event, worker, or prefork for Apache.
Apache has three different processing modules — mpm_prefork, mpm_worker, and mpm_event —each of which has different features:
- mpm_prefork spawns a separate process for each connection. This is a good processor for components like PHP that may not always be thread-safe. However, performance degrades when there are more requests than processes.
- mpm_worker is kind of a combination of event and prefork. It spawns processes that can each manage multiple threads.
- mpm_event is like worker, but better optimized to handle keep-alive connections.
NGINX uses an event-driven architecture rather than a multi-threaded architecture. This means NGINX can handle multiple requests simultaneously without having to create a new thread for each request. This makes it more efficient and scalable than Apache.
Apache vs. NGINX: Flexibility
Apache uses dynamically loadable modules to add additional functionality or features. It comes with more than 50 official modules, and third-party modules are available. Being dynamic, they can be built and loaded at any time.
NGINX has more than 100 third-party modules, but they are not dynamically loaded, so they need to be compiled within the core software itself. Dynamically loaded modules are available with the commercial offering, NGINX Plus.
Apache vs. NGINX: Support
Apache provides support through the documentation, a users list, and an IRC chat channel. Support can also be purchased through third-party providers.
F5 (the current owners of NGINX) provides support for their commercial offering, NGINX Plus. There is also documentation, an IRC channel, and other resources.
Back to topWhen to Use Apache vs. NGINX
Both Apache and NGINX can be used to meet a multitude of application requirements. However, there are a few considerations for enterprise deployment that teams should keep in mind.
Apache Use Cases
Apache is better for shared hosting environments, due to its ability for non-privileged users adding configuration files for individual directories. It is also good for dynamic content, such as PHP or Java applications.
NGINX Use Cases
NGINX generally has better performance for static files, so it is typically used for a front-end or a proxy to back-end services. This makes is ideal for load balancing requests, and it is often used as an ingress controller in Kubernetes. However, it can't serve dynamic content by default, and needs to proxy all dynamic content requests to a back-end server.
Using Apache and NGINX Together
Sometimes it's not a question of Apache vs. NGINX, as some situations call for Apache and NGINX. It is common to use NGINX as a reverse proxy in front of Apache. NGINX would typically be used to serve static content, passing any other requests back to Apache to serve dynamic content.
Back to topFinal Thoughts
When it comes to Apache vs. NGINX, one is not necessarily better than the other. Depending on your environment and application needs, it may make more sense to use Apache or NGINX, or use them together as described above.
In the most recent State of Open Source Report, Apache HTTP and NGINX were the top two most popular open source web infrastructure tools, with usage within a percentage point of each other (36% NGINX vs. 35% Apache).
By industry, technology organizations slightly prefer NGINX (50% NGINX vs. 43% Apache), whereas Apache has the edge in government and public services (46% Apache vs. 42% NGINX).
Editor's Note: This blog was originally published on February 16, 2023. It was most recently updated on February 28, 2024.
Looking For NGINX or Apache HTTP Support?
OpenLogic can help you configure your web server properly and provide SLA-backed technical support for your entire open source infrastructure. Available 24/7/365, our Enterprise Architects have 15+ years of experience with open source enterprise deployments.
Additional Resources
- Blog - Top Open Source Infrastructure Technologies from the State of Open Source Report
- Blog - NGINX Performance Tuning: Top 5 Tips
- Blog - NGINX Setup and Configuration Tips
- Blog - Web Server Security Best Practices
- Blog - Web Server vs. Application Server: What's the Difference?
- Blog - Tomcat vs. NGINX
- On-Demand Webinar - The State of Open Source Web App Development
- Course - NGINX Training
- Guide - Overview of Open Source Web Infrastructure