Apache and Nginx are the two most common open source web servers in the world. Together, they are responsible for serving over 50% of traffic on the internet.
While Apache and Nginx share many qualities, but they have some advantages and disadvantages while deploying an website.
Before choosing the right server you must know their capabilities and then choose as per the requirement.
Introduction
Before we dive into the differences between Apache and Nginx, let’s take a quick look at the background of these two projects and their general characteristics.
Apache
The Apache HTTP Server was created by Robert McCool in 1995 and has been developed under the direction of the Apache Software Foundation since 1999. Since the HTTP web server is the foundation’s original project and is by far their most popular piece of software, it is often referred to simply as “Apache”.
The Apache web server was the most popular server on the internet from at least 1996 through 2016. Because of this popularity, Apache benefits from great documentation and integrated support from other software projects.
Apache is often chosen by administrators for its flexibility, power, and near-universal support. It is extensible through a dynamically loadable module system and can directly serve many scripting languages, such as PHP, without requiring additional software.
Nginx
In 2002, Igor Sysoev began work on Nginx as an answer to the C10K problem, which was an outstanding challenge for web servers to be able to handle ten thousand concurrent connections. Nginx was publicly released in 2004, and met this goal by relying on an asynchronous, events-driven architecture.
Nginx has since surpassed Apache in popularity due to its lightweight footprint and its ability to scale easily on minimal hardware. Nginx excels at serving static content quickly, has its own robust module system, and can proxy dynamic requests off to other software as needed.
Nginx is often selected by administrators for its resource efficiency and responsiveness under load, as well as its straightforward configuration syntax.
Highlights
- Apache was released first in 1995, then came Nginx in 2004.
- Both are used by large Fortune 500 companies around the globe.
- Nginx market share has been steadily growing for years.
- In some instances, Nginx has a competitive edge in terms of performance.
Performance
A. Static Content
Apache
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
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.
B. Dynamic Content
Apache
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.
Nginx
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.
Highlights
- Apache serves static content using the file-based method, but Nginx is best in terms of serving static content as it serves faster than Apache
- Apache processes dynamic content within the server. Nginx is little slower than Apache as it doesn’t processes dynamic content within the server.
Winner:
Static: As far as Static content is concerned, Nginx overpasses Apache.
Dynamic: Both are great at processing Dynamic content.
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
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
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. But currently it doesn’t support dynamic modules.
Configuration
Apache and Nginx differ significantly in their approach to allowing overrides on a per-directory basis.
Apache
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.
Since these files reside within the content directories themselves, when handling a request, Apache checks each component of the path to the requested file for an .htaccess file and applies the directives found within. 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.
While the above examples can all be configured in the main Apache configuration file, .htaccess files have some important advantages. First, since these are interpreted each time they are found along a request path, they are implemented immediately without reloading the server. Second, it makes it possible to allow non-privileged users to control certain aspects of their own web content without giving them control over the entire configuration file.
This provides an easy way for certain web software, like content management systems, to configure their environment without providing access to the central configuration file. This is also used by shared hosting providers to retain control of the main configuration while giving clients control over their specific directories.
Nginx
Nginx does not interpret .htaccess files, nor does it provide any mechanism for evaluating per-directory configuration outside of the main configuration file. Apache was originally developed at a time when it was advantageous to run many heterogeneous web deployments side-by-side on a single server, and delegating permissions made sense. Nginx was developed at a time when individual deployments were more likely to be containerized and to ship with their own network configurations, minimizing this need. This may be less flexible in some circumstances than the Apache model, but it does have its own advantages.
The most notable improvement over the .htaccess system of directory-level configuration is increased performance. For a typical Apache setup that may allow .htaccess in any directory, the server will check for these files in each of the parent directories leading up to the requested file, for each request. If one or more .htaccess files are found during this search, they must be read and interpreted. By not allowing directory overrides, Nginx can serve requests faster by doing a single directory lookup and file read for each request (assuming that the file is found in the conventional directory structure).
Another advantage is security related. Distributing directory-level configuration access also distributes the responsibility of security to individual users, who may not be trusted to handle this task well. Keep in mind that it is possible to turn off .htaccess interpretation in Apache if these concerns resonate with you.
Using Apache and Nginx Together
After reviewing the benefits and limitations of both Apache and Nginx, you may have a better idea of which server is more suited to your needs. In some cases, it is possible to leverage each server’s strengths by using them together.
The conventional configuration for this partnership is to place Nginx in front of Apache as a reverse proxy. This will allow Nginx to handle all client requests. This takes advantage of Nginx’s fast processing speed and ability to handle large numbers of connections concurrently.
For static content, which Nginx excels at, files or other directives will be served quickly and directly to the client. For dynamic content, for instance PHP files, Nginx will proxy the request to Apache, which can then process the results and return the rendered page. Nginx can then pass the content back to the client.
This setup works well for many people because it allows Nginx to function as a sorting machine. It will handle all requests it can and pass on the ones that it has no native ability to serve. By cutting down on the requests the Apache server is asked to handle, we can alleviate some of the blocking that occurs when an Apache process or thread is occupied.
This configuration also facilitates horizontal scaling by adding additional backend servers as necessary. Nginx can be configured to pass requests to multiple servers, increasing this configuration’s performance.