Web server is essential for all server to serve the content over the internet. There are a lot of web servers available in the market. The two Major web servers are Apache and Nginx. A lot of people are confusing themselves about choosing the one among both of them. In this article, I am going to compare Apache and Nginx which will give the answer to your confusion. Both of these web servers can’t be replaced by each other. They have their uniqueness. You have to choose any of the web server based on the requirement. In this article, we will also see the nginx vs apache difference.
Here: I am going to compare Apache and Nginx with following factors
Connection Handling architecture
Static vs Dynamic content
Distributed vs Centralized configuration
File vs URI based interpretation
Using both of them together.
Apache was initially created by Robert McCool. The year of creation was 1995. After that, Apache has been developed by Apache software foundation from 1999. The actual organization is HTTP web server. But the foundation is now referred with the name of Apache. The Product name overrides the organization name. It becomes very famous after its launch since then the Apache foundation is benefiting from the documentation and integration with software. Apache is the first choice for many server administrators due to its power, compatibility and support for many platforms. The Apache is easily extensible through loading the dynamic module. Apache process many interpreted languages and it does not use any other external software for that.
The Origin of Nginx is an interesting thing. The Nginx was created by Igor Sysoev. Nginx was created as a solution for the c10K problem. At that time, it was very hard to serve a large number of concurrent users. Even the Apache was struggling to handle the traffic. It was a challenge to serve 10,000 concurrent visitors at a time. Igor Sysoev created Nginx as a solution to this. It was created to handle a large number of concurrent users with limited resources. Nginx performed well and managed a large number of concurrent users with minimal resources. After that, it becomes very famous. The Administrators who handle the server with a large number of concurrent visitors started to use Nginx. Nginx Performs well even at heavy loads. Nginx has some advanced features. They are Reverse Proxy Load Balancing Many people want to know difference between apache and nginx server. Let us see them here.
1) Connection Handling Architecture
Let us see how Apache and Nginx are handling the connections with their client.
Apache provides various multiprocessing modules to handle the client request.
The administrator can easily change the Multi-Processing Module(MPM).
mpm_prefork: This module creates processes with a thread for each.
Each thread handles a single request. This module works faster until the number of request crosses the number of processes.
The process will consume memory so it makes the server slow down.
It is not easy to scale up when the process is high.
This module is best for working with other components which are not created to depend on threads.
PHP is not fit to work with thread. This mpm_prefork module is best to work with PHP.
This is the only safe way to work with PHP using mod_php apache module.
mpm_worker: This module creates processes each with multiple threads.
Many threads mean handling of many connections. This module is much more efficient than Prefork Module.
This time new client request can be caught by a thread. So, there is no need of waiting for the process to become free.
MPM worker is scaling better than Prefork Module.
mpm_event: This module is somewhat similar to mpm_worker.
In the mpm_event a connection is kept alive regardless of whether the request is being made as long as the connection exists.
mpm_event also handles threads of Keep-Alive connection requests.
It sets dedicated threads of keep-alive connection and sends the remaining requests to other threads.
This helps the Process to prevent being filled with keep-alive connection requests.
Keepalive connection should be stable and fixed with the specific threads. So the dedicated thread is the must.
You could see the availability of request handling algorithms in Apache. T
he variation is created for different usages.
Web administrator can choose the module based on the server capability.
However, The internet is growing so the concurrency is also growing with that.
The Nginx is created with the purpose of serving more requests simultaneously without any interruption with minimal resource.
The Apache was struggling to handle a large number of user concurrently, this is where Nginx came in and performed well.
After that, it started to gain attention from webmasters.
The Nginx is preferable for servers with a large number of visitors.
Nginx is best in delivering the static content and almost works two times faster than Apache.
Nginx is growing faster as many webmasters started to using it for its performance. But, at the same time, the Usage of Apache web server also becoming low.
Most of the time Apache web server is replaced by Nginx server.
Nginx is working completely in a different way when compared to Apache web server.
Nginx has one main master process.It is recommended to have one master process per CPU.
The master process creates multiple worker processes. Each worker process has only one thread.
This is where Nginx differs from Apache.
Single Nginx threads serve multiple requests whereas in Apache single thread only serves one connection.
The worker process connections are placed in event loop with other connections and all of them are served asynchronously.
Once the request is served completely, the event will be removed from the loop.
Since a single thread handles the multiple connections, No need of creation of the additional process which leads to memory and CPU consumption.
So, Nginx serves without going down even when the load is heavy.
Checkout this forum to see the nginx vs apache 2017 comparison.
2) Static vs Dynamic content
Static content serving:
Nginx is leading in this contest and performed 2.5 times faster than Apache in serving static content when tested with 1000 concurrent connections.
In another test with 512 concurrent connections to Nginx, it performed 2 times faster than Apache at the same time Nginx consumed 4% less memory.
So, if you are serving the static content to a lot of concurrent users, then you should choose Nginx as the new web server.
When you go for Nginx vs Apache WordPress, both of them are almost giving same performance for
Dynamic Content Serving:
Now, most of the website is running based on Dynamic content. It essential for any web server to serve the dynamic content faster.
During the test for the Dynamic content, Nginx and Apache both performed equally.
They also consumed same memory and CPU Resource.
There are a lot of websites uses CMS such as WordPress, Joomla and Drupal.
The web server also needs to serve the dynamic content from them.
Here, both of the web servers are doing the same task “that is forwarding dynamic content requests to php-fpm and wait for its process and response the request, then they serve the content to the client.
When you go for Nginx vs Apache php, the Apache directly works with PHP whereas the Nginx uses php-fpm.
Both web servers are depending the third party to process the request.
Until then they serve the static content without waiting for the process to complete.
So, you can choose any of the web servers to serve dynamic contents as they don’t show any difference in the performance.
3) Operating System
Apache supports all the Linux and Unix-like operating system and works on them perfectly.
Apache also completely supports the Microsoft Windows.
Nginx also highly supports Linux and Unix-like operating system.
Only provides some support to windows. However, this can’t stop from running the web server.(Most of the hosting servers are running on Linux)
The main difference between Apache and Nginx is directory level configuration.
In Apache web server, you can use the directives to control a web directory. The directives will be hidden in a file called .htacess.
== The .htaccess file is powerful and lets you control some aspects of web server directly without accessing the web server configuration file.==
The directives written within this file are so effective and the result reflects immediately without restarting the server.
You have to be careful while handling the .htaccess. Even a small mistake can cause trouble to your site.
Apache will always look for .htaccess file in the parent directory. Once it finds the directory, It reads the directives
Here are the few things which you can control using .htaccess.
It allows the decentralized configuration by giving access to CMS.
This will help server administrators to give some specific access to the webmasters in the shared hosting.
So, the webmasters can have some control over their site and they can further customize it.
Nginx does not interpret any such files. Nginx does not provide any option like .htaccess.
All the access are centralized and you have to make all changes in the main configuration file.
It may seem a backdrop for Nginx when compared to Apache, but it has the advantage.
In Apache, the server will look for .htaccess directory in the corresponding directory and it entirely searches for the hidden files in the parent directory.
It is a time-consuming process. After finding the. htaccess file, the server will interpret the directives and fetches the directory contents to the user.
If there are more .htaccess file, they all should be interpreted.
This process will happen again and again for the each request made to the server.
Nginx directly reads the directories and deliver the content to the user according to its main configuration directives.
This saves a lot of time and delivers the content faster. This is one of the reason for Nginx to work faster.
Also, the security is high since the Configuration is centralized.
The web server has all the control over the configuration. Individual users have to contact the admin for any changes that have to be made.
It also prevents the user from making suspicious activities with .htaccess file.
5) Working Model
You can make a request to Apache that can be interpreted as a Physical resource on a file system.
The request can also be made in the form of URI.
Apache has been using Directory blocks and location blocks. File blocks are also used instead of Directory blocks.
The location block is used for more abstract resources.
Apache is entirely created as a web server. It interprets the request as File system resources.
To get a file, Apache begins the search from document root with the host and port number along with the request.
Apache has many options when the search for file end up in not finding the file. You can use alias directive to represent the alternative location of the file.
If you want to use Location block instead of the file system, you have to use URI.
You can use the available regular expression to configure the file system with flexibility.
Apache can work as both underlying file system and web space. The file system is the one the Apache depends on the most.
The .htaccess is one such example for the file system dependency of Apache.
Nginx works both as a web server and proxy. The Architecture of Nginx differs a lot from apache. Due to this, Nginx uses the URI mainly and uses the file system when required.
You can see that the way of Nginx configuration file is created and interpreted.
Nginx does not allow you to specify the configuration for a directory. Instead, it directly parses the URI itself.
Nginx uses two primary blocks. They are server block and location blocks.
The server block interprets the hostname and the location block interprets URI which comes after the host and port.
The request is interpreted as URI, not as location file system.
If a user is requesting for a static file, the Nginx selects the server block and location block. Also, the Nginx appends the document root with the URI.
The function may seem similar.
But actually, it is not. Parsing the request as URI instead of File system lets the Nginx function easily as web, email and proxy server.
Nginx configuration responds the request as mentioned in the configuration.
You can extend both Apache and Nginx using the module. The function style of Apache and Nginx Differs a lot from each other.
you can use the module according to the requirement. Load and unload the modules easily to meet the requirement of the server.
You can turn off and turn on the Modules easily. Each module provides different functionalities.
Apache is very old has been serving for a long time in the internet world. A lot of libraries have been created for Apache.
You can use install any of the modules to change and add functions in the Apache.
For example, The mod_php adds the PHP interpreter to each worker in Apache.
The modules are not only serving the dynamic content. They have more functionality than you think. Here is some of them.
Hardening the server
The core functionality can be extended using the dynamic module.
Nginx also has various modules to support the user requirement. But, there is a major difference between the Apache and Nginx modules.
Nginx modules are not dynamically loadable. You have to add them yourself in the core software.
This is not an easy task for many users to build their server from the core with their selected packages.
It will be much harder for people who use the conventional packaging system which consists the commonly required modules.
If you have a specific requirement, then you have to build the server from the core using required modules.
Nginx Modules can be instructed to do specific tasks so that you can get what you want. This is also considered as a more secure way by many users.
Nginx modules have many functionalities same as Apache modules. Here is some of them.
8) Support, Compatibility, Ecosystem, and Documentation
If you are using a software, you always need the support and help to use that for a long time.
It is the responsibility of the software provider too.
Apache is a popular web server and it has been in the market for a very long time.
It has been serving in the internet world for more than two decades.
This very long time existence of Apache is its greatest advantages.
Being the first famous vendor in the internet Apache has been widely used by many people.
There are a lot of documentation are available.
The third party documentation is also available. A lot of web software tool is available to bootstrap in the Apache environment.
A lot of third party supports are available for Apache since it has been evolving in the market for a long time and holds more share in the market.
Server administrators familiar with Apache.
It is also used in the shared hosting for its one of the best feature .htaccess file.
The distributed configuration environment gives access to the webmasters to take control over their website.
Nginx has been serving the internet for more than a decade. Nginx has the third place in the web server market.
A lot of people have been choosing Nginx as their web server.
The growth of the Nginx is so fast and it very high when compared to Apache growth throughout the last decade.
Due to the speed, Many server admins adopted Nginx.
Initially, the detailed documentation of the Nginx is not available in the English language.
The initial documentation was in the Russian Language.
After some time, the interest of the project has grown and the documentation has been converted to English. The Nginx itself has a lot of administration resource to handle the Nginx.
A large number of third parties also providing resources. This makes a one to choose Nginx.
A lot of third-party application gives you an option to auto-configure Nginx.
If you don’t have Nginx on your server, they automatically download and configure Nginx for their application to work on your machine.
If you need to install the Nginx for any third party applications, it will be easy for you.
It is because they provide clear documentation about the requirement for that applications.
It makes your job very easy to create a custom Nginx server.
9) Using Apache and Nginx together
You have seen the difference and limitations of Nginx and Apache. Now you should have decided which one is the best suit for your server.
When you look at Nginx vs apache reverse proxy, both are giving best performance for dynamic content.
You can use both of them together to benefit from both web server software.
You can use Nginx as a reverse proxy in front of Apache.
The main advantage of Nginx is concurrency and speed of delivering static content.
If a user requests for static content, it can be served immediately by Nginx directly. If a user requests for dynamic content, the Nginx forwards the request to the Apache.
Apache process the request and forwards to the content to the Nginx. Then Nginx forwards the content to the user.
This is a suggested and well-working setup for many people. The Nginx and Apache together share the workload.
First Nginx receives the request. If it has the ability to process the request, It will respond to the request.
If Nginx doesn’t have the ability to handle the request, then it will be forwarded to Apache.
By making Nginx handle the part of the request, you will avoid the situation of Apache server threads are being occupied and become unable to respond the request.
The biggest advantage is you can add more backend server to this setup. Nginx has the capability to handle many servers and it can even load balance between them.
It can help you to scale up when required.
You have seen the above comparison between Nginx and Apache. The one thing you have to remind is “Nothing can entirely fulfill your requirements”.
You have to choose the web server based on your major and important requirement.
As mentioned above, you can use the Nginx and Apache combination.