intermediate
Nginx reverse proxy and static serving
Route requests to upstream services, serve immutable static assets, preserve trusted headers, and separate public file delivery from app logic.
Nginx efficiently serves static files and proxies dynamic requests to upstream Node processes. Immutable hashed assets get long cache lifetimes; HTML entry points stay short-lived or uncached.
server {
listen 443 ssl;
root /var/www/dist;
location /assets/ {
expires 1y;
add_header Cache-Control "public, immutable";
try_files $uri =404;
}
location /api/ {
proxy_pass http://node_upstream;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
Separate public file delivery from application logic — Nginx handles gzip/Brotli and TLS cheaper than Node for static bytes.
On interviews: explain `try_files` for SPAs; which headers must pass to Node for correct URL generation; why `proxy_pass` trailing slash changes path rewriting.
Common pitfalls: caching `index.html` aggressively; missing `X-Forwarded-Proto` breaking HTTPS redirects in apps; serving user uploads from the same root as build artifacts.
The trade-off is balancing simplicity, performance, safety, and operability — name which axis you optimized and what cost you accepted.
Checklist:
- Long cache only for fingerprinted static assets.
- Proxy API paths with trusted forwarding headers.
- Use upstream blocks for multiple Node instances.
- Keep uploads outside the deploy artifact tree.