Why Is Nginx So Fast? Inside Its Process Model and Event Architecture

This article explains Nginx’s high performance by dissecting its multi‑process architecture, event‑driven model, HTTP connection handling, modular design, and I/O mechanisms, comparing it with Apache, and clarifying common questions about worker processes, maximum connections, and concurrency.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Why Is Nginx So Fast? Inside Its Process Model and Event Architecture

Nginx is a free, open‑source, high‑performance HTTP server, reverse proxy, and IMAP/POP3 proxy.

Nginx’s Process Model

Nginx runs one master process and multiple worker processes. The master process manages workers, receives external signals, forwards them to workers, monitors worker status, and restarts workers if they exit unexpectedly. Workers are equal and handle network requests. The number of workers is configured in nginx.conf, usually matching the CPU core count to maximize utilization while avoiding excessive context switches.

HTTP Connection Establishment and Request Handling

When Nginx starts, the master loads the configuration, initializes listening sockets, and forks worker processes. Workers compete for new connections; the winner completes the three‑way handshake, establishes a socket, and processes the request.

Why Nginx Is Fast and Handles High Concurrency

Nginx uses a multi‑process, asynchronous non‑blocking architecture based on I/O multiplexing (epoll on Linux, kqueue on FreeBSD). The request flow is: establish connection → read request → parse request → process request → send response, which maps to low‑level socket read/write events.

Event Handling Model

Requests are received line‑by‑line, headers and optional body are read, then processed, and a response consisting of status line, headers, and body is generated. Nginx follows this standard HTTP server workflow.

Modular Architecture

Event module: provides a platform‑independent event framework (e.g., ngx_events_module, ngx_event_core_module, ngx_epoll_module).

Phase handler modules: handle client requests, e.g., ngx_http_static_module for static files.

Output filter modules: modify response content, such as adding footers or rewriting URLs.

Upstream modules: implement reverse proxy, forwarding requests to backend servers.

Load‑balancer modules: select a backend server based on configured algorithms.

Common Questions

Nginx vs Apache

Nginx: I/O multiplexing (epoll/kqueue), high performance, high concurrency, low resource usage.

Apache: blocking I/O with multi‑process/thread, more stable, richer module ecosystem.

Maximum Connections

Each worker handles up to worker_connections connections, limited by the OS file‑descriptor limit ( ulimit -n).

Total connections = worker_processes × worker_connections.

When used as a reverse proxy, each client connection also opens a backend connection, effectively halving the maximum concurrent connections.

HTTP Request and Response

Request: request line (method, URI, HTTP version), headers, optional body.

Response: status line (HTTP version, status code), headers, body.

I/O Models

I/O multiplexing: a single thread monitors many sockets and reads/writes the ready ones.

Blocking I/O + multithreading: a thread per request; simpler but incurs higher context‑switch overhead.

Select, Poll, Epoll

All three are synchronous I/O mechanisms. Select uses a fixed‑size fd_set (max 1024 descriptors) and linear scanning. Poll replaces fd_set with an array of pollfd, removing the descriptor limit. Epoll registers events per descriptor, supports unlimited descriptors, and avoids linear scans by maintaining a ready list.

// select system call
int select(int maxfdp, fd_set *readfds, fd_set *writefds, fd_set *errorfds, struct timeval *timeout);
// poll system call
int poll(struct pollfd fds[], nfds_t nfds, int timeout);
Source: http://ningg.top/nginx-series-principle/
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

performanceNginxEvent-drivenIO MultiplexingProcess Model
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.