Nginx guide
What Nginx does in production
Nginx handles HTTP/HTTPS connections with an event-driven architecture that scales well under concurrency. It serves files from disk, reverse-proxies to app servers (Gunicorn, uWSGI, Node), and can load-balance across upstreams. TLS termination, gzip compression, caching, and rate limiting are configured in plain-text config files — no modules to compile for most use cases.
How a request is handled
When a client connects, the typical workflow is:
- TCP accept — a worker process accepts the connection on a
listensocket - TLS handshake — if HTTPS, the certificate is selected by SNI hostname
- Server block match — Nginx picks a
server { }block usinglisten,server_name, and default-server flags - Location match — within the server block, the best-matching
locationhandles the URI (prefix, exact=, regex~) - Handler — static file (
root/try_files), proxy (proxy_pass), or redirect (return) - Response — logged to
access_log; errors go toerror_log
If no server_name matches, Nginx uses the default server
for that port — usually the first server block defined.
Configuration layout
- Debian/Ubuntu —
/etc/nginx/nginx.confincludessites-enabled/; enable sites with symlinks fromsites-available/ - RHEL/CentOS/Rocky —
/etc/nginx/nginx.confincludesconf.d/*.conf - Snippets — shared fragments in
/etc/nginx/snippets/(SSL params, proxy headers)
Test before reload: nginx -t. Apply:
systemctl reload nginx.
Key directives to know
server_name— hostnames this server block responds toroot/alias— filesystem path for static files (aliasreplaces the matched location path)proxy_pass— upstream URL for reverse proxyingupstream— named backend pool with load-balancingtry_files— try paths in order; common pattern for SPAsclient_max_body_size— upload limit (413 if exceeded)include— pull in other config files
Location matching priority
Nginx chooses the most specific match: exact = beats prefix (longest
wins), then regex in definition order, then general prefix. A common mistake is
a broad location / catching requests meant for location /api/
— prefix length and ordering matter.
Learning resources
- Nginx documentation — nginx.org/en/docs (official reference)
- Server names — nginx.org/en/docs/http/server_names (how server blocks are selected)
- ngx_http_proxy_module — nginx.org/en/docs/http/ngx_http_proxy_module (reverse proxy directives)
- Beginner's guide — nginx.org/en/docs/beginners_guide (configuration walkthrough)
Practice scenarios
Hands-on Nginx scenarios on live Linux VMs: nginx