Master Nginx: From Basics to Advanced Load Balancing and High‑Concurrency Techniques
This comprehensive guide explains what Nginx is, its key advantages, typical use cases, request handling flow, high‑concurrency model, forward and reverse proxy concepts, directory layout, configuration modules, cookie vs session, differences with Apache, static‑dynamic separation, CDN integration, load‑balancing algorithms, rate limiting, health checks, compression, worker process tuning, and common status codes.
What is Nginx?
Nginx is a lightweight, high‑performance reverse‑proxy web server supporting HTTP, HTTPS, SMTP, POP3 and IMAP, capable of handling tens of thousands of concurrent connections.
Key Advantages
Cross‑platform, simple configuration
Non‑blocking, high concurrency (2‑3 万 connections, up to 5 万 measured)
Low memory consumption (10 workers ≈150 MB)
Open‑source and low cost
High stability
Built‑in health‑check
Typical Use Cases
Standalone HTTP server for static files
Virtual hosting
Reverse proxy and load balancing
API gateway and security management
How Nginx Handles Requests
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
}The master process creates listening sockets, forks worker processes, workers accept connections, register read/write events, and close connections when finished.
High‑Concurrency Model
Nginx uses an asynchronous, non‑blocking event model (epoll) so a few worker processes can serve many requests, unlike a one‑process‑per‑request model.
Forward and Reverse Proxy
Forward proxy fetches resources on behalf of a client; reverse proxy receives client requests and forwards them to backend servers, hiding the origin.
Directory Structure
/usr/local/nginx/
├── conf/
│ ├── nginx.conf
│ └── mime.types
├── html/
│ └── index.html
├── logs/
│ ├── access.log
│ └── error.log
├── sbin/nginx
...nginx.conf Modules
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}Cookie vs Session
Cookies are stored in the client browser; sessions are stored on the server (file, DB, Redis) and hold sensitive data.
Nginx vs Apache
Nginx is lightweight, event‑driven and handles many connections with few resources, while Apache uses a process‑oriented model that consumes more memory and CPU per connection.
Static‑Dynamic Separation and CDN
Separate static resources (HTML, JS, CSS, images) from dynamic content, serve static files via Nginx or a CDN to improve performance and reduce backend load.
Load‑Balancing Algorithms
Round‑robin (default)
Weight
IP‑hash
Fair (third‑party)
URL‑hash (third‑party)
upstream backserver {
server 1112;
server 1113 weight=2;
ip_hash;
}Rate Limiting
Implemented with limit_req_zone (leaky‑bucket) and limit_conn_zone (concurrent connections).
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/m;
limit_conn_zone $binary_remote_addr zone=myip:10m;Health Checks
Use ngx_http_proxy_module and ngx_http_upstream_module or the third‑party nginx_upstream_check_module to monitor backend servers.
Compression
http {
gzip on;
gzip_min_length 1k;
gzip_comp_level 2;
gzip_types text/plain application/javascript text/css;
gzip_vary on;
}Worker Processes
Set worker_processes to the number of CPU cores; each worker handles many connections without costly context switches.
Status Codes
Examples: 499 (client closed), 502 (bad gateway) – often caused by upstream timeouts, buffer limits, or long‑running scripts.
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.
Raymond Ops
Linux ops automation, cloud-native, Kubernetes, SRE, DevOps, Python, Golang and related tech discussions.
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.
