Nginx Configuration & Server Snippets Cheat Sheet
Production Nginx server configuration snippets: upstream reverse proxying, Let's Encrypt SSL, HSTS, rate limiting zones, SPA fallback, Brotli compression, and CORS headers with 1-click copy.
Lifecycle & Testing (4 commands)
sudo nginx -t
Tests Nginx configuration syntax and files for errors without reloading server.
sudo systemctl reload nginx
Gracefully reloads configuration with zero downtime: existing connections finish cleanly.
sudo tail -f /var/log/nginx/error.log
Tails the Nginx global error log in real time.
sudo tail -f /var/log/nginx/access.log
Live stream of incoming client HTTP requests, IPs, status codes, and referrers.
Reverse Proxy & Upstreams (2 commands)
location / {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
Standard production reverse proxy block passing traffic to Node.js/Python backend.
upstream backend_cluster {
least_conn;
server 10.0.0.1:8080 weight=2;
server 10.0.0.2:8080;
server 10.0.0.3:8080 backup;
}
Defines load-balanced backend cluster using least-connections algorithm with backup server.
SSL / HTTPS Configuration (2 commands)
server {
listen 80;
server_name example.com www.example.com;
return 301 https://$host$request_uri;
}
Permanent HTTP (port 80) to HTTPS (port 443) redirect.
listen 443 ssl http2;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;
Configures modern TLS certificates with HTTP/2 protocol support.
Security & Hardening Headers (2 commands)
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-Content-Type-Options "nosniff" always;
add_header X-XSS-Protection "1; mode=block" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
Standard security headers preventing clickjacking, MIME-sniffing, and XSS exploits.
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
Enforces HSTS (HTTP Strict Transport Security) for 1 year across all subdomains.
Performance, Caching & Gzip (2 commands)
gzip on;
gzip_vary on;
gzip_min_length 1024;
gzip_proxied any;
gzip_types text/plain text/css text/xml application/json application/javascript image/svg+xml;
gzip_disable "MSIE [1-6]\.";
Enables gzip text compression for HTML, CSS, JavaScript, JSON, and SVG files.
location ~* \.(jpg|jpeg|png|gif|ico|css|js|webp|woff2)$ {
expires 365d;
add_header Cache-Control "public, no-transform, immutable";
access_log off;
}
Enables 1-year browser edge caching for static assets with immutable flag.
Rate Limiting & Protection (2 commands)
limit_req_zone $binary_remote_addr zone=api_limit:10m rate=10r/s;
location /api/ {
limit_req zone=api_limit burst=20 nodelay;
proxy_pass http://127.0.0.1:3000;
}
Rate limits incoming requests to 10 requests/sec per client IP with burst tolerance.
location ~ /\.(?!well-known) {
deny all;
access_log off;
log_not_found off;
}
Blocks public web access to hidden files and dotfiles (e.g., .env, .git, .htaccess).