Production-ready nginx configurations for FastAPI, Next.js, static sites, WebSocket proxying, and load balancing. Every config is security-hardened with modern TLS, HSTS, rate limiting, and compression.
nginx-configs/
├── snippets/
│ ├── ssl.conf # TLS 1.2/1.3, HSTS, OCSP stapling
│ ├── security-headers.conf # CSP, X-Frame-Options, etc.
│ ├── gzip.conf # Compression settings
│ └── rate-limiting.conf # Rate limit zone definitions
├── sites/
│ ├── fastapi-app.conf # FastAPI reverse proxy + WebSocket
│ ├── nextjs-app.conf # Next.js with ISR + static caching
│ └── static-site.conf # Static SPA with aggressive caching
├── load-balancing/
│ └── upstream.conf # Multiple backends, least_conn, health checks
├── streaming/
│ └── websocket.conf # WebSocket upgrade proxy
└── setup.sh # Deploy script
sudo mkdir -p /etc/nginx/snippets
sudo cp snippets/*.conf /etc/nginx/snippets/# Dry run first
./setup.sh sites/fastapi-app.conf --dry-run
# Deploy
./setup.sh sites/fastapi-app.confThe script copies to sites-available, symlinks to sites-enabled, runs nginx -t, and reloads.
Include in any server {} block with SSL. Enforces TLS 1.2/1.3, disables weak ciphers, enables OCSP stapling, and adds HSTS with preload.
server {
listen 443 ssl http2;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem;
include snippets/ssl.conf; # <-- adds TLS hardening + HSTS
}SSL Setup with Let's Encrypt:
sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d example.com -d www.example.com
# Certbot auto-renews. Test renewal:
sudo certbot renew --dry-runInclude in server {} or location {}. Adds:
X-Frame-Options: SAMEORIGIN— clickjacking protectionX-Content-Type-Options: nosniff— MIME sniffing protectionReferrer-Policy: strict-origin-when-cross-originContent-Security-Policy— restrict resource originsPermissions-Policy— disable camera/mic/geolocation
Customize the CSP for your app's needs.
Include in http {} block (in nginx.conf) or server {}. Compresses text, JSON, JS, CSS, SVG. Skips already-compressed formats.
Must be included in the http {} block (not server {}), typically in /etc/nginx/nginx.conf:
http {
include /etc/nginx/snippets/rate-limiting.conf;
...
}Then use in server {} or location {}:
location /api/ {
limit_req zone=api burst=10 nodelay;
limit_conn addr 20;
}
location /login {
limit_req zone=login burst=3 nodelay;
}| Zone | Rate | Use for |
|---|---|---|
| api | 30/min | General API endpoints |
| login | 5/min | Auth endpoints |
| general | 100/min | Public pages |
Reverse proxy for a FastAPI (or any Python ASGI) app:
- HTTP → HTTPS redirect
- TLS 1.2/1.3 + HSTS
- Rate limiting on all routes, stricter on
/login - WebSocket proxy at
/ws(long timeouts, no buffering) proxy_next_upstreamretries on error- Keepalive connections to upstream
Prerequisites: FastAPI running on ports 8000 and 8001 locally.
Reverse proxy for Next.js (port 3000):
/_next/static/— cache forever (hashed filenames)/_next/image— 7-day cache/api/— no cache, short timeout/— ISR: Next.js controlsCache-Control, nginx passes it through- Non-www → www redirect
Pure static file serving (e.g. Vite, CRA, Hugo):
- SPA fallback:
try_files $uri $uri/ /index.html - Hashed JS/CSS — 1 year cache,
immutable - Images — 30 day cache
- HTML — no cache
- Pre-compressed Brotli support (
brotli_static on)
Three upstream pools:
api_cluster—least_connwith 3 primaries + 1 backup, passive health checks viamax_failsstatic_cluster— round-robinws_cluster—ip_hashfor WebSocket sticky sessions
Passive health check tuning:
server 10.0.1.10:8000 max_fails=3 fail_timeout=30s;After 3 failures in 30 seconds, the server is marked down for 30 seconds.
Key settings for WebSocket proxying:
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_buffering off;
proxy_read_timeout 3600s;Uses ip_hash for sticky sessions (WebSocket connections must stay on the same backend).
- Replace all
example.comwith your actual domain - Install SSL certificates with certbot before enabling configs
- Move
rate-limiting.confincludetohttp {}block innginx.conf - Customize
Content-Security-Policyfor your app's actual script/style sources - Restrict
/nginx_statusto your monitoring IP - Verify
client_max_body_sizematches your upload limits