In this post, I’m going to discuss in detail the difference between Apache and NGINX server.
Which is the fastest web server?
What are their key benefits and limitations?
Which one should you go for?
With ServerGuy Magento hosting or any other hosting plan you get both the servers depending upon your choice and requirement.
Let’s dive right into it.
Apache Vs NGINX Difference
The main difference between Apache and NGINX lies in their design architecture. Apache uses a process-driven approach and creates a new thread for each request. Whereas NGINX uses an event-driven architecture to handle multiple requests within one thread.
We will cover the following topics on the post:-
- What is Apache HTTP Server?
- What is NGINX Web Server?
- Architecture
- Performance
- OS Support
- Configuration
- Interpretation
- Modules
- Flexibility
- Security
- Support
- Final Takeaway
- When Choose Apache over NGINX?
- When Choose NGINX over Apache?
- Apache and NGINX- Together
What is Apache HTTP Server?
Apache HTTP Server is an open-source cross-platform web server which is also known as “httpd” and Apache. It was developed by the Tim Berners Lee and released in 1995. Apache became popular quickly, and now it is maintained by the Apache foundation.
Some interesting facts about Apache are:-
- Apache Software Foundation manages its brand and services.
- The server is developed and maintained by an open community of developers under ASF.
- Mostly run on Linux, Apache powers around 46% of all the website around the world.
- It is the key component of the LAMP (Linux, Apache, MySQL, PHP) stack.
What is NGINX Web Server?
What is NGINX?
Pronounced as “Engine X” —it is a web server that was released in 2004 by Igor Sysoev. Today, it is a lot more than just a webserver! NGINX is more popular than Apache Web Server now, due to various factors we will see in this article.
Here’s what you should know about Nginx server:
- At first, people mostly used Nginx as an Apache supplement.
- It was used mostly for serving static files, but today, it has evolved as a complete web server that deals with the full spectrum of server tasks.
- Today, Nginx is often used as a reverse proxy, load balancer, mail proxy and for HTTP caching.
Both Apache and Nginx are the most common web server for Linux. Together, they serve more than 50% of traffic on the web.
P.S. While Apache and Nginx share many qualities, they are different in many areas. Each excels in its own way and has its own uses and scenarios.
To know the differences and reach out to a conclusion, let’s go through the detailed, end-to-end comparison below. We have also mentioned the winner of each point of comparison at the end of each point.
Basic Architecture
When it comes to Apache vs Nginx, the fundamental difference lies in their design architecture. It means they differ in the actual way they handle connections and traffic and respond to different traffic conditions.
Apache implies a process-driven approach and the NGINX follows an event-driven architecture.
Apache
- Process Driven Approach
- Creates a new thread for each request.
Apache follows a multi-threaded approach. It provides a variety of multiple processing modules. These pre-modules are basically of three types of request handling algorithm. Each is meant for different server-needs.
The MPMs (Multi-Processing Modules) provides a flexible architecture for choosing different connections and different handling algorithms.
Also, different versions of Apache 2 employ different procession modules.
The three main Apache MPMs are:
- Process (Pre-fork) MPM
- Worker MPM
- Event MPM
Old school Apache (2.2) uses mpm_worker, mpm_prefork, and mod_php. Whereas Apache 2.4 (new apache) is configured to use mpm_event, php-fpm.
By default, Apache 2.2 is configured in Pre-fork mode (mpm_prefork). It responds to a set number of processes, each of which can serve a single request at a time.
In other words, Apache creates a new thread every time to handle each connection request.
Thread: A thread is the smallest sequence of programmed instructions that can be managed independently by a scheduler. In most cases, a thread is a component of a process.
However, Apache’s basic architecture can lead to heavy resource consumption, thereby, can cause issues with the server (eg- slow speed).
NGINX
- Event-Driven approach
- Handles multiple requests within one thread
Nginx employs an event-driven architecture and deals with the requests asynchronously.
It was designed to use a non-blocking event-driven connection handling algorithm. Thus, its process can handle thousands of connection (requests) within one processing thread. Such connections process modules allow Nginx to work very fast and wide with limited resources.
Furthermore, you can use Nginx on low power systems, and with the systems that operate under heavy loads.
Winner:
NGiNX – It has a lightweight structure, and much faster architecture than that of Apache.
Performance
Taking about Apache vs Nginx, both of the web servers process static and dynamic contents differently. They say Nginx is better when it comes to processing static content, let’s see if that’s true!
2.1 Static Content
Apache
- Serves static content using the file-based method
Static content or files are typically files stored on disk on the server computer, for example, CSS files, JavaScripts files or images. Apache handles static content using its conventional file-based method.
NGINX
- At serving static content, Nginx is the king!
As Nginx’s design architecture is better equipped to handle the load, it is much faster when it comes to serving the static content.
It performs 2.5 times faster than Apache according to a benchmark test running up to 1,000 simultaneous connections.
Nginx serves the static resources without PHP having to know about this. On the other hand, Apache handles all those requests with that costly overhead. This makes Nginx more effective and less demanding on the system resources.
This small image shows the no. of static content requests processed per second. Nginx clearly surpasses Apache here!
2.2 Dynamic Content
Apache
- Processes dynamic content within the server
Apache can process dynamic content within the web server itself without having to rely on any external components. So, it can handle your creeds itself.
Talking about Apache vs Nginx Performance: Nginx, if not better, is almost equal when dynamic content processing is considered.
Here’s the comparison of the dynamic content request numbers per second. Looks like there is little difference.
NGINX
- It doesn’t process dynamic content
Talking about dynamic content, Nginx can’t process it within the web server as Apache does. All the requests with dynamic web page content are passed to an external process (eg- PHP-FPM) for execution.
Nginx waits for the final content to come back and delivers it back to the client. Refer to this image for a clear idea:
NGINX can serve the dynamic content when used with SCGI handlers and FastCGI module.
[Tweet “NGINX can serve the dynamic content when used with SCGI handlers and FastCGI module.”]
P.S. This process sounds a bit complicated. However, it somewhat works in its favor and makes Nginx faster.
Winner:
Static: As far as Static content is concerned, Nginx overpasses Apache.
Dynamic: Both are great at processing Dynamic content.
OS Support
OS support can be an important point to consider, especially when comparing Apache vs Nginx. But, both of them are almost similar here.
Apache
- Supports all Unix-like systems including Linux and BSD.
- It fully supports MS-Windows
Apache runs on all kinds of Unix-like systems (e.g., Linux or BSD) and has full support for Microsoft Windows.
NGINX
- Supports almost all Unix-like OS
- Supports Windows partially.
It also runs on several modern Unix-like systems and has some support for Windows, but its Windows performance is not as strong as that of other platforms.
Winner:
Apache comes out the winner here.
Distributed / Centralized Configuration
Apache vs Nginx is a well-deserved topic. Their configuration makes them different from each other and makes them equally interesting. Let’s see whose configuration is easier and faster.
Apache
- Allows additional configuration on a per-directory basis via .htaccess files.
This architecture allows non-privileged users to control certain aspects of their website without granting them permission to edit the main config. This is big!
NGINX
- Doesn’t allow additional configuration
It, on the other hand, has a downside. It doesn’t provide additional configuration. However, it works in your favour as this increases the performance.
By not allowing directory configuration It can serve request faster than Apache. It doesn’t need to search for .htaccess files and interpret the req made by the user.
Winner:
Apache if configuration is considered; NGiNX if speed is.
Request Interpretation
In Apache vs Nginx debate, the method of interpreting requests is an interesting topic for comparison. Both of them process and interpret requests in an entirely different manner.
Their different methods make them unique and also make one a bit better than the other. Let’s explore how!
Apache
- Passes File system location
Provides the ability to interpret req. As a physical resource on the file system location that may need more abstract evaluation. It passes requests as file system locations.
Of course, Apache does use URI locations, but they are generally for more abstract resources. And while creating or configuring a Virtual host, Apache use directory blocks under the document root.
This preference for file system locations can also be seen in the use of .htaccess files for overriding specific directory configurations.
NGINX
- Passes URI to interpret requests
Nginx was created to be both a web server and a reverse proxy server. Due to the architecture request for these, Nginx works primarily with your eyes. Translating to the system when necessary.
It doesn’t provide a mechanism for specifying config. For file system directory, instead passes their URI itself. Passing requests as URI instead of file system locations allow Nginx to function easily in both web and proxy server. It configures simple by laying out how to respond to diff requests patterns.
It doesn’t check the file system until it is ready to serve the request It explains why it doesn’t implement any form of .htaccess files.
This very design of interpreting requests as URI locations allows Nginx to easily function as not only a web server but also as a proxy server, load balancer, and HTTP cache.
Also, in the Apache vs Nginx contest, NGINX again wins when it comes to the transfer rate (the speed at which data was sent from the server to the client). And in most cases, Nginx wins by a fair amount for the 500/100.
Winner:
Seems like Nginx has won because of its quicker interpretation and response.
Feature Modules
Both are extensible through module systems. But the way they work is different. Let’s compare the feature modules of both the web servers: Apache vs Nginx!
Apache
- 60 official dynamically loadable modules that can be turned On/Off
Apache server 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.
Its module system allows you to dynamically load or unload modules to satisfy your needs. Its modules can be turned ON or Off to add or remove functionality and to hook into the main server.
In short, Apache has several feature modules to meet your need but many of them are not used often.
NGINX
- 3rd Party core modules (not dynamically loadable)
Nginx, on the other hand, is selected and compiled into the course of the 3rd party plugins. Not dynamically loadable. The modules are useful though, they allow you to dictate what you want from your server by only including the functionality you intend to use.
It is considered much more secure than Apache server as arbitrary components can be hooked in the server.
Also, NGINX provides all of the core features of a web server, without sacrificing the lightweight and high‑performance qualities that have made it successful.
Note: Apache is like Microsoft Word and Nginx is like notepad. How? Apache has a million options but you only need a few. NGINX does those ‘a few’ things and does them 50 times faster than Apache.
Winner:
NGiNX – It’s less yet important features and modules make it lighter, smarter and better web server than Apache.
Flexibility
Flexibility is one of the most important concerns when it comes to a web server. Apache vs Nginx flexibility has some interesting differences.
Apache
- Supports customization of web server through dynamic modules.
Customizations to the web server can be done through riding modules. Apache has had dynamic module loading for the longest time, so all Apache modules support this.
NGINX
- Not flexible enough to support dynamic modules and loading.
This is, however, not the case for NGINX. At the beginning of 2016, NGINX got support for dynamic module loading; previously, NGINX required the admin to compile the modules into the NGINX binary.
Most modules do not yet support dynamic loading, but over time they probably will.
Winner:
Apache – It clearly leads on this point.
Security
Apache vs Nginx security is again a debated topic. Well, both of these web servers offer great extended security for its C-based code base.
So, users, relax!
Apache
- Great security.
Apache makes sure that all the website that runs on its server are safe from any harm and hackers.
Thereby it offers configuration tips for DDoS attack handling, as well as the mod_evasive module for responding to HTTP DoS, DDoS, or brute force attacks.
Talking about security and Apache, you must read our detailed post on cPanel security. A secured cPanel means a secured website.
NGINX
- Better security with the smaller codebase.
The NGINX code base, however, is significantly smaller by several orders of magnitude, so that is definitely a big plus from a forward-thinking security perspective. NGINX also has a list of recent security advisories. Read a post on Mitigating DDoS attacks on Nginx blog.
Winner:
Nginx – It is considered more secure
Support
Support is something that every customer looks keenly for. It can make or break your user experience. While comparing Apache vs Nginx support, there seems to be no big difference.
Apache
- Community support is done through mailing lists, IRC, and Stack Overflow.
Commercial Apache support is available from a number of third-party companies, such as OpenLogic, but no official list is maintained by the Apache Foundation. Apache server is meant to offer great support to all of its users.
NGINX
- Community support through mailing lists, IRC, Stack Overflow, and a forum.
The company behind NGINX offers a commercial product called NGINX Plus, which has support for a set of extra features regarding load-balancing, media streaming, and monitoring.
Winner:
It’s a tie! The support is almost same in both. Both web servers are great.
Here’s the comparison chart on Apache vs NGINX as a summary:
Final Takeaway
Both Apache and Nginx can’t be replaced by each other, they have their strong and weak points. And after going over the benefits, limitations, and differences, you may already have an idea of the best web server for you.
Well, in our post on Apache vs Nginx, 5 out of 9 points have been won by NGINX, 2 points go to Apache, and 2 were ‘draw’ between Apache and Nginx. So, we can clearly see, NGINX has a lead over Apache.
Still confused about which server is the better solution for you? Let’s find out – when to choose which!
When Choose Apache over NGINX?
i) Apache .htaccess
NGINX does not support something like the Apache’s .htaccess file. However, with Apache, you get an advantage to give the non-privileged users control over some important aspects of their website.
- The users, obviously, are not permitted to edit the main configuration.
- Using .htaccess files, you can override system-wide settings on a per-directory basis.
- For optimal performance, include these .htaccess directives in the main configuration file(s).
- In a shared hosting environment, Apache works better because of its .htaccess configuration.
P.S. For dedicated hosting or VPS Nginx stays the best option.
Read This: VPS vs Dedicated Hosting: Difference you Need to Know
ii) In case of functionality limitations – use Apache
Nginx has some core modules that are very important. Howbeit, there are some functionality limitations with Nginx.
In case of some limitations or needing to use extra modules that are not supported by Nginx, you might want to choose Apache instead.
When Choose NGINX over Apache?
i) Fast Static Content Processing
Nginx can perform a much better job at handling the static files from a specific directory.
Also, the upstream server processes don’t get blocked because of the heavy, multiple static content requests as Nginx can process them concurrently. This significantly improves the overall performance of backend servers.
Nginx is incessantly trying to serve its users a better experience. It has shown some remarkable growth in the year 2018. Take a glance at the key takeaways from 2018 on the Nginx blog.
ii) Great for High Traffic Websites
If we talk about the speed and how many clients can be served on a high load, Nginx will always shine as a winner over Apache.
This makes Nginx significantly lightweight and great for server resources. This is why most of the web developers prefer Nginx over Apache.
Especially, the e-stores today hire a Magento developer that knows how to work on a high-traffic website, and adept in working on Nginx.
In a nutshell, when it comes to serving a website with a large volume of traffic, there is no beating Nginx.
Or, Use Both of them -Together!
Yes, Apache and Nginx can be friends too!
It is possible to use each server’s strengths by using them together.
You can use Nginx in front of Apache as a server proxy (as shown in the image below). This takes advantage of Nginx fast processing speed and ability to establish large no, of connection simultaneously.
For static connections, Nginx will serve the files quickly to the clients. For dynamic content, for example, Php files, Nginx reverse proxy server will proxy their request to Apache which can then process their results and return their rendered page.
Nginx can then pass the final content to the client. Also, it allows you to have a very functional webserver to serve your clients (the large volume of users) very fast.
Here comes the end of Apache vs Nginx contest!
Bottom Line:
Deciding which web server you will use, Nginx or Apache, is an important step in setting up your website.
Both the solutions are capable of handling diverse workloads and working with other software to provide a complete web stack.
We hope this end-to-end comparison guide helped you pick out the best web server for you. Want to gain some extra tips? Get in touch with us!
19 thoughts on “Apache Vs NGINX – Which Is The Best Web Server for You?”
thanks for sharing nice vs between apache and nginx
How the f*ck is “Better security with the smaller codebase.” an argument? Smaller doesn’t mean better.
“Nginx serves the static resources without PHP having to know about this. On the other hand, Apache handles all those requests with that costly overhead.”
You do know that mod_php is a plugin module, and that not everyone nor every site use php?
“How the f*ck is “Better security with the smaller codebase.” an argument? Smaller doesn’t mean better.”
In a small system, it is easier to search through, analyze, and understand code. For any security concern, one can quickly analyze it and fix it. Building larger systems requires larger teams and longer-lasting projects, which bring additional overhead and risks of (project) failure.
“You do know that mod_php is a plugin module, and that not everyone nor every site use php?”
PHP is used by 78.9% of all websites with a known server-side programming language. So almost 8 out of every 10 websites that you visit on the Internet are using PHP in some way.
I hope you find your answer.
Nginx is great for my tiny VPS 😀
migration to nginx is a bit more challenging
Yes, indeed but with a great support team, everything is possible.
I think litespeed perform best as a web server.
Thanks, Ruby for an insightful and detailed article. This has cleared a lot of my doubts.
I agree with this article, I love Nginx, but sometimes migrate to Nginx is more challenging especially when the apps are using .htaccess. lol
Thanks, @Ruby for sharing your great research about these web servers.
I was an Apache user, but I was forced to move to Nginx and I didn’t regret it. Nginx is great for my tiny VPS.
Thank! Very helpful.
My quick question (i’m a beginner), i want to develop restaurant food-ordering mobile apps, is Nginx better or Apache a better solution for this?
Thanks for this overview. As a webmaster, I am very happy with the performance of Apache, but I am getting more inquiries about Nginx. It’s great to see competition making both standards better.
There are other reasons playing for NginX. When you decide to go with Docker, NginX is winner here:
– official php docker images built on Alpine are only Fpms – which can be better connected with NginX
– NginX is easier to configure with docker
– overall docker images Apache + Php vs NginX + FPM are much bigger
– since Nginx is loosely coupled with Php FPM, you can scale them separately – more NginX instance + one Php-fpm or multiple Php-fpm instances and one Nginx – depending on your load
– once you learn NginX you can easily use NginX as a reverse proxy for your projects too
There are of course disadvantages:
– nginx solution plays better with front controller based php apps – for multi entry point apps (more than one php script executed) it has somehow schizophrenic deploy logic
Thanks for sharing your deep insights.
Thanks, @Ruby for sharing your great research about these web servers.
I was using Apache in my dedicated VPS server for a long time and had been struggling with performance issues but after reading this article I have decided to switch to nginx as none of the positive points you mentioned for Apache are need by my server for now.
Very cool stuff, thanks for all the details! I’m getting ready to set up my own dedicated server and this guide has been helpful.