Why Nginx Outperforms Apache in High‑Concurrency Scenarios
The article compares Nginx and Apache, showing how Nginx’s event‑driven, single‑threaded architecture and efficient use of memory and I/O allow it to handle tens of thousands of concurrent connections with far lower resource consumption than Apache’s prefork or worker models.
Nginx, despite being only a few years old, has quickly become the dominant web server because its handling of high‑concurrency static requests is markedly more efficient than Apache (httpd), easily solving the classic C10K problem.
In the author’s tests, an Nginx + PHP (FastCGI) setup sustained over 30,000 concurrent connections, roughly ten times the capacity of an equivalent Apache configuration.
By contrast, a 4 GB server running Apache in prefork mode could only manage about 3,000 concurrent connections before exhausting more than 3 GB of RAM, leading to crashes when the MaxClients limit was approached.
The Nginx + PHP server used only 150 MB for ten Nginx worker processes and 1.28 GB for 64 php‑cgi processes, staying under 2 GB total; reducing the php‑cgi pool to 25 processes cuts memory usage to about 500 MB while maintaining performance.
Even under 30,000 concurrent connections, PHP scripts served by Nginx + FastCGI remain fast.
1. Apache’s Three Processing Models
Apache provides three Multi‑Processing Modules (MPMs): prefork, worker, and event.
prefork : multiple processes, each handling one request; uses the select mechanism for notifications.
worker : multiple threads per process; each thread handles a request while still using select, allowing more simultaneous requests.
event : asynchronous I/O model; a single process/thread can handle many connections using an event‑driven (epoll) mechanism.
1.1 prefork
Prefork is the default MPM on Unix when no explicit --with-mpm is given. It spawns separate child processes for each request, providing strong isolation and stability.
1.2 worker
Worker mixes processes and threads, offering higher scalability with lower resource overhead than pure process models while retaining the stability of process isolation.
1.3 event
Event uses a callback‑driven approach where a single process can manage many connections, keeping the process idle when no work is pending and thus supporting massive concurrency with minimal resources.
2. Improving Web‑Server Concurrency
Key techniques include:
Thread‑based handling: one process creates multiple threads, each serving a request.
Event‑driven models: a single process handles many requests via epoll‑style notifications.
Asynchronous I/O (AIO) on disk.
Memory‑mapped I/O (mmap) to avoid redundant copying of page data.
Nginx supports all of these features, which is why its documentation claims support for 50,000 concurrent connections.
3. Nginx’s Advantages
Traditional process‑ or thread‑based servers suffer from blocking I/O, high context‑switch overhead, and inefficient CPU/memory utilization.
Nginx adopts a modular, event‑driven, asynchronous, single‑threaded, non‑blocking architecture, heavily leveraging multiplexing and event notification mechanisms.
Each worker process contains only one thread but can handle thousands of concurrent connections via an efficient run‑loop.
4. Nginx Architecture
Nginx runs a master process (root) and several worker processes; when caching is enabled, additional cache‑loader and cache‑manager processes appear. All processes are single‑threaded and communicate via shared memory.
In high‑concurrency environments, Nginx is a solid alternative to Apache.
Installation and configuration are straightforward, bugs are rare, and the server can run continuously for months without restarts, even allowing seamless version upgrades.
5. Solving the C10K Problem
Comparison of I/O models:
5.1 select
Limited by a maximum number of file descriptors; scanning all descriptors incurs linear overhead.
5.2 poll
Removes the descriptor limit but still requires linear scanning.
5.3 epoll (used by Nginx)
Provides event‑driven readiness notifications and returns only the number of ready descriptors, using memory‑mapped arrays to avoid copying large descriptor tables. This dramatically improves performance, though it is Linux‑only.
Overall, Nginx’s design and use of epoll make it far more efficient for handling massive numbers of simultaneous connections compared with Apache.
Laravel Tech Community
Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.