Complete Guide to Modern Nginx Server Configurations
Nginx powers over a third of all websites on the internet, yet crafting a secure, performant `nginx.conf` file manually remains intimidating. A single missing `proxy_set_header` or improper `try_files` order can cause broken WebSocket connections, routing loops, or security vulnerabilities like clickjacking.
1. Reverse Proxy for Modern App Frameworks
When routing incoming requests to a Node.js, Next.js, FastAPI, Flask, or Go service running on an internal port (e.g. `127.0.0.1:3000`), Nginx acts as the gatekeeper. It terminates SSL, buffers large client uploads, and forwards client headers.
location / {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Host $host;
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;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}2. Single Page Application (SPA) Routing & Caching
Single Page Applications compiled with Vite, Webpack, or CRA produce static assets with unique content hashes. Our SPA preset automatically adds 1-year immutable caching headers to fonts, images, and compiled bundles while preventing caching on `index.html`.
3. Hardened Security Headers & Gzip Compression
Default Nginx installations omit essential security headers. Our generator automatically emits `X-Frame-Options: SAMEORIGIN` (mitigates clickjacking), `X-Content-Type-Options: nosniff` (stops MIME sniffing), and `Strict-Transport-Security` (forces HTTPS).