How Nginx Handles Millions of Concurrent Connections: Architecture Explained
This article explains why Nginx is renowned for high performance and details the architectural components—master and worker processes, event‑driven non‑blocking I/O, memory pooling, load‑balancing, caching, modular design, and proxy mechanisms—that enable it to sustain millions of simultaneous connections.
What Is Nginx?
Nginx is an open‑source, high‑performance web and reverse‑proxy server that can run 24/7, supports hot deployment, and handles a wide range of protocols (TCP, UDP, SMTP, HTTPS, etc.). It is free, commercially usable, and widely adopted by major Chinese and global internet companies.
Nginx Architecture
How Nginx Supports Million‑Level Concurrency
Master and Worker Processes
When Nginx starts, it creates a master process that manages one or more worker processes. The master schedules workers but does not handle network traffic directly; each worker independently processes thousands of connections.
# ps -ef|grep nginx
root 6324 1 0 09:06 ? 00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx
nobody 6325 6324 0 09:06 ? 00:00:00 nginx: worker process
root 6327 1244 0 09:06 pts/0 00:00:00 grep --color=auto nginxEvent‑Driven Model
Nginx’s event loop consists of an event collector, an event sender, and an event processor. The collector gathers I/O requests from workers, the sender forwards them to the processor, which handles the events asynchronously, allowing many connections to be served simultaneously.
Event collector: gathers I/O requests from workers.
Event sender: dispatches I/O events to the processor.
Event processor: executes the actual I/O handling.
Non‑Blocking I/O and Multiplexing
Requests are processed with non‑blocking I/O, preventing a worker from waiting on slow operations. Nginx employs I/O multiplexing mechanisms such as select, poll, and especially epoll on Linux, enabling a single thread to manage many sockets.
Memory Pool Management
Nginx uses pre‑allocated memory pools to avoid frequent allocations and de‑allocations, reducing overhead and keeping performance stable under heavy load.
Load Balancing
As a reverse proxy, Nginx can distribute client requests across multiple backend servers using various strategies.
upstream server_pools {
server 192.168.1.100:8888 weight=5;
server 192.168.1.101:9999 weight=5;
server 192.168.1.102:6666 weight=5;
}
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://server_pools;
}
}Round‑robin (default)
Least connections
Fastest response time
IP hash (session persistence)
Caching
Nginx can cache static files or upstream responses, dramatically reducing backend load. The following directives illustrate a typical cache configuration.
proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=hot_cache:10m inactive=60m max_size=1g;
proxy_cache hot_cache;
proxy_cache_valid 200 302 1d;
proxy_cache_key $host$uri$is_args$args;
proxy_cache_min_uses 3;
proxy_cache_lock on;
proxy_cache_lock_timeout 3s;
proxy_no_cache $cookie_nocache $arg_nocache $arg_comment;
add_header Cache-Status $upstream_cache_status;Modular Design
Nginx’s core is extensible via modules: core, standard HTTP, optional HTTP, mail, and third‑party modules. Modules can be compiled in or loaded dynamically, allowing features such as SSL, advanced load balancing, and custom logging without affecting the core event loop.
Proxy Mechanism
When acting as a reverse proxy, Nginx receives client requests, forwards them to upstream servers, and can apply load‑balancing and caching on the fly, improving overall throughput and latency.
http {
upstream product_server { 127.0.0.1:8081; }
upstream admin_server { 127.0.0.1:8082; }
server {
location / { proxy_pass http://product_server; }
location /admin/ { proxy_pass http://admin_server; }
}
}Conclusion
Through a combination of a master/worker process model, event‑driven non‑blocking I/O, efficient memory pooling, flexible load‑balancing and caching, and a modular architecture, Nginx can reliably handle millions of concurrent connections, making it a cornerstone of modern high‑traffic web infrastructures.
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.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
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.
