Nginx Cheatsheet
Configuration Structure
Use this Nginx reference while you build software engineering projects, review code for technical interview prep, or polish examples for a software engineer resume.
Top-level Layout
# Global / main context user www-data; worker_processes auto; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; server { listen 80; server_name example.com; location / { root /var/www/html; } } }
Contexts nest strictly: main → events → http → server → location.
Context Reference
| Context | Purpose | Lives inside |
|---|---|---|
main | Global settings (user, PID, worker count) | top level |
events {} | Connection-handling tuning | main |
http {} | All HTTP/HTTPS config; shared defaults | main |
server {} | Virtual host — one block per domain/port | http |
location {} | URI-specific rules | server (or another location) |
upstream {} | Backend pool for proxying/load balancing | http |
stream {} | TCP/UDP proxy (separate from http) | main |
mail {} | Mail proxy | main |
Common Main-context Directives
user nginx; # Worker process OS user worker_processes auto; # Number of workers (auto = CPU count) worker_rlimit_nofile 65535; # Max open file descriptors per worker error_log /var/log/nginx/error.log warn; # Path + minimum level pid /run/nginx.pid;
events {} Directives
events {
worker_connections 1024; # Max simultaneous connections per worker
use epoll; # I/O method: epoll (Linux), kqueue (BSD)
multi_accept on; # Accept all new connections at once
}Max total connections = worker_processes × worker_connections.
server {} Block Essentials
server {
listen 80; # IPv4
listen [::]:80; # IPv6
server_name example.com www.example.com;
root /var/www/example;
index index.html index.htm;
access_log /var/log/nginx/example.access.log;
error_log /var/log/nginx/example.error.log;
}server_name Matching Order
- Exact name (
example.com) - Leading wildcard (
*.example.com) - Trailing wildcard (
example.*) - Regex (
~^(www\.)?example\.) - Default server (
default_serverflag, or first defined)
listen 80 default_server; # Catch-all for unmatched hostnames
Include and Snippets
# Inline include — path is relative to nginx prefix or absolute include /etc/nginx/conf.d/*.conf; include snippets/ssl-params.conf; # Typical snippet file: /etc/nginx/snippets/ssl-params.conf ssl_protocols TLSv1.2 TLSv1.3; ssl_ciphers HIGH:!aNULL:!MD5;
Inheritance Rules
- Directives in outer contexts are inherited by inner contexts.
- Inner context values override the outer value for that directive.
- Most directives are not merged — the innermost wins entirely.
http {
gzip on; # inherited by all server blocks
server {
gzip off; # overrides for this server only
}
}Variables
$host # Host header (or server_name if absent) $request_uri # Full URI including query string $uri # Current URI (may be rewritten) $args # Query string $remote_addr # Client IP $server_name # Matched server_name value $scheme # http or https $request_method # GET, POST, etc.
Use in directives: return 301 https://$host$request_uri;