Why Is Nginx So Fast? Inside Its Process and Event Model
This article explains Nginx's high performance by detailing its multi‑process architecture, asynchronous event‑driven I/O, request handling flow, module types, comparison with Apache, maximum connection calculations, HTTP request/response structure, and the differences among select, poll, and epoll.
Nginx Process Model
Nginx runs with one master process and multiple worker processes. The master manages workers, receives external signals, monitors worker status and restarts workers that exit unexpectedly. Workers are equal and handle network requests; the number of workers is configured in nginx.conf, usually matching CPU cores to maximize utilization while avoiding excessive context switching.
HTTP Connection Establishment and Request Handling
When Nginx starts, the master loads the configuration file, initializes listening sockets, and forks multiple workers. Workers compete for new connections; the winner completes the three‑way handshake, establishes a socket, and processes the request.
Why Nginx Achieves High Performance and Concurrency
Multi‑process + asynchronous non‑blocking I/O (epoll).
Full request lifecycle: connection → read → parse → process → respond.
All operations map to socket read/write events.
Event Processing Model
Nginx modules are categorized as:
event module : provides an OS‑independent event framework (e.g., ngx_events_module, ngx_event_core_module, ngx_epoll_module).
phase handler : handles client requests and generates responses (e.g., ngx_http_static_module).
output filter : modifies output content, such as adding footers or rewriting image URLs.
upstream : implements reverse proxy, forwarding requests to backend servers and returning their responses.
load‑balancer : selects a backend server based on load‑balancing algorithms.
Common Questions
Nginx vs Apache
Nginx : uses I/O multiplexing (epoll/kqueue), high performance, high concurrency, low resource consumption.
Apache : blocking I/O with multi‑process/thread, more stable, richer module ecosystem.
Maximum Connections
Total connections = number of workers × max connections per worker (controlled by worker_connections and the system limit ulimit -n).
When acting as a reverse proxy, each client connection also opens a backend connection, effectively halving the maximum served connections.
HTTP Request and Response Structure
Request consists of a request line (method, URI, HTTP version), headers, and an optional body. Response consists of a status line (HTTP version, status code), headers, and a body.
I/O Models
To handle many requests, servers can use I/O multiplexing (single thread monitors many sockets) or blocking I/O with multiple threads (one thread per request).
I/O multiplexing (select/poll/epoll) reduces context switches and memory usage, suitable for high concurrency and long‑living connections.
Blocking I/O + threads is simpler but incurs higher thread‑management overhead.
Select vs Poll vs Epoll
select : limited to 1024 file descriptors, linear scan of fd_set, copies fd_set between user and kernel.
poll : replaces fd_set with an array, removes the 1024‑fd limit but still copies data.
epoll : event‑driven, registers interest per fd, no inherent fd limit, avoids linear scans.
Concurrency Capability
After optimization, Nginx can handle roughly 10 000–30 000 concurrent connections, depending on memory and CPU resources.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
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.
