image from nginx.com
Web server is a key factor that directly has an effect on the performance of the website. It is also an issue that each webmaster consider when choosing the operation environment for a website. There are many web servers available, and Apache, Nginx and Microsoft's IIS are the mainstream. This article is to compare between Apache and Nginx, so that users can choose their own Web server.
Apache was founded in 1995 and has been developed by Apache software foundation since 1999. Apache is flexible and efficient with rich extension modules. It has an active community, and has become a mainstream open-source Web server around the world.
Nginx is a free and open source Web server written by Igor Sysoev, a Russian Software Engineer. Since its launch in 2004, Nginx has focused on high performance, high concurrency and low memory usage. Its characteristics such as load balancing, caching, access, bandwidth control and efficient integration with various applications have made it popular among users.
Apache VS Nginx
Although Apache and Nginx have different backgrounds, their purpose is the same, which is to receive user requests, process requests, and finally return the results to the user.
The biggest difference between Apache and Nginx is how they handle connections. Apache provides a series of multiprocessing modules through which to use the resources of the operating system, manage the process and thread pool, and control the processing of user requests.
Apache provides three kinds of multiprocessing modules: mpm_prefork, mpm_worker and mpm_envent.
- mpm_prefork：A module generates subprocesses, and each subprocess is a single thread, and each thread links to a request. So it is a one-to-one relation. When the number of requests is more than the number of processes, the performance of the server will be unsatisfactory.
- mpm_worker：Unlike prefork, the worker neutron process is multithread, and each thread manages a user connection. The number of threads is more than the number of processes, which also means that a new connection can instantly get a free thread rather than waiting for the process to be available.
- mpm_event：The module is similar to worker, and the difference is that the event can handle long connections (keep-alive) to avoid the waste of resources for a thread being requested for a long time, and also enhance the request processing capability in case of a high concurrency.
Nginx, unlike Apache, is an asynchronous, nonblocking and event-driven method. Its operational process is single thread, and each thread processes a large number of user requests asynchronously. The following is the operational principle of Nginx:
image source: http://www.aosabook.org/en/nginx.html
Apache can handle both static and dynamic content with its built-in dynamic scripting languages (including PHP, Python, and Perl)for parsing and executing. It does not require an external processor. There is no doubt that dynamic content processing is a pain for Nginx. Nginx is not efficient in processing dynamic content, and it needs external processors. Therefore, Apache might be a better option for you, if your site has many dynamic features. Although Nginx is not good at dynamic content processing, but its static content processing is efficient.
Apache has a lot of modules, and it is stable with less BUG and powerful dynamic content processing. Nginx is lightweight, taking less resource with load balancing, high concurrent processing, and efficient static content processing.
There is a reason for its existence. Apache and Nginx have their own pros as webservers. They would not replace each other in a short future. The user should take the background of their application into consideration, and choose one according to their requirements and circumstances.