Skip to content

About

Production-ready nginx configs for FastAPI, Next.js, WebSocket, load balancing — SSL, rate limiting, security headers

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

nginx-configs

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.

Structure

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

Quick Start

1. Install snippets globally

sudo mkdir -p /etc/nginx/snippets
sudo cp snippets/*.conf /etc/nginx/snippets/

2. Deploy a site config

# Dry run first
./setup.sh sites/fastapi-app.conf --dry-run

# Deploy
./setup.sh sites/fastapi-app.conf

The script copies to sites-available, symlinks to sites-enabled, runs nginx -t, and reloads.

Snippets

snippets/ssl.conf

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-run

snippets/security-headers.conf

Include in server {} or location {}. Adds:

  • X-Frame-Options: SAMEORIGIN — clickjacking protection
  • X-Content-Type-Options: nosniff — MIME sniffing protection
  • Referrer-Policy: strict-origin-when-cross-origin
  • Content-Security-Policy — restrict resource origins
  • Permissions-Policy — disable camera/mic/geolocation

Customize the CSP for your app's needs.

snippets/gzip.conf

Include in http {} block (in nginx.conf) or server {}. Compresses text, JSON, JS, CSS, SVG. Skips already-compressed formats.

snippets/rate-limiting.conf

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

Site Configs

sites/fastapi-app.conf

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_upstream retries on error
  • Keepalive connections to upstream

Prerequisites: FastAPI running on ports 8000 and 8001 locally.

sites/nextjs-app.conf

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 controls Cache-Control, nginx passes it through
  • Non-www → www redirect

sites/static-site.conf

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)

Load Balancing (load-balancing/upstream.conf)

Three upstream pools:

  • api_cluster — least_conn with 3 primaries + 1 backup, passive health checks via max_fails
  • static_cluster — round-robin
  • ws_cluster — ip_hash for 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.

WebSocket (streaming/websocket.conf)

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).

Security Checklist

  • Replace all example.com with your actual domain
  • Install SSL certificates with certbot before enabling configs
  • Move rate-limiting.conf include to http {} block in nginx.conf
  • Customize Content-Security-Policy for your app's actual script/style sources
  • Restrict /nginx_status to your monitoring IP
  • Verify client_max_body_size matches your upload limits

About

Production-ready nginx configs for FastAPI, Next.js, WebSocket, load balancing — SSL, rate limiting, security headers

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages