Appearance
Reverse Proxies and CDNs
14. What is a reverse proxy, and why might you use Nginx?
The short answer
A reverse proxy is a server that sits in front of your app servers and receives every client request, then forwards it to the app and relays the response back. Clients only ever see the proxy. Nginx is the most common one, because it is very fast at handling many connections. You use it to terminate TLS, serve static files, compress responses, buffer slow clients so Node is not tied up, enforce size limits and timeouts, cache responses, route paths to different services, and hide the app servers from the internet. A forward proxy is the opposite: it sits in front of clients, such as a corporate web filter.
| Job | Why do it in Nginx, not Node |
|---|---|
| TLS termination | One place for certificates; very efficient |
| Static files | Served from disk with sendfile, no JavaScript involved |
| Slow clients | Nginx buffers the upload or response, so a Node process is not held for a slow phone connection |
| Compression | Offloads CPU from the app |
| Limits | Body size, request rate, timeouts, before Node sees the request |
| Routing | /api to Node, / to the static front end, /admin blocked |
| Caching | Micro-cache public responses for a second or two |
Forward versus reverse:
| Forward proxy | Reverse proxy | |
|---|---|---|
| Sits in front of | Clients | Servers |
| Acts for | The user | The website |
| Example | Company web filter, VPN | Nginx, a CDN, a cloud load balancer |
15. What is SSL/TLS termination at a load balancer or reverse proxy?
The short answer
TLS termination means the proxy or load balancer decrypts incoming HTTPS traffic, holds the certificates, and forwards plain HTTP, or newly encrypted traffic, to the app servers. The app no longer handles certificates or the CPU cost of encryption, and certificates are managed and rotated in one place. The app must then learn that the original request was HTTPS from X-Forwarded-Proto, which needs trust proxy in Express. If the network between the proxy and the app is not trusted, re-encrypt to the backends, or use mTLS, instead of sending plain HTTP inside.
| Mode | Proxy to app | Use when |
|---|---|---|
| Termination | Plain HTTP | Trusted private network |
| Re-encryption | New TLS connection | Compliance or untrusted internal network |
| Passthrough | Encrypted bytes forwarded untouched | App must see the client's TLS, such as for mTLS to the app |
The bug this causes: Express thinks every request is plain HTTP, so it refuses to set Secure cookies and redirect loops appear. app.set('trust proxy', 1) fixes it (trust proxy).
16. What is a Content Delivery Network (CDN), and when is it useful?
The short answer
A CDN is a network of servers in many cities, called edge locations, that cache your content close to users. A visitor in Mumbai gets your JavaScript bundle from a server in Mumbai instead of your origin in London, which cuts latency from hundreds of milliseconds to tens, and your origin serves only the cache misses. CDNs are useful for static assets, images and video, public pages and API responses that many users share, and as a shield: they absorb traffic spikes and DDoS attacks and often include a web application firewall. Examples are Cloudflare, CloudFront, Fastly and Akamai.
| Content | Through a CDN? |
|---|---|
| JS, CSS, fonts, images with hashed names | Yes, cached for a year |
| Product images and video | Yes |
| Public pages, public API lists | Yes, short TTL |
| Logged-in pages and personal API responses | Pass through only, private or no-store |
| Uploads | Usually directly to object storage, served back through the CDN |
What else a CDN gives you: TLS at the edge, HTTP/3, compression, DDoS protection, bot filtering, rate limits, and image resizing. The caching headers that control it are explained in browser and CDN caching.
17. How should static files be cached in production?
The short answer
Give every built asset a content hash in its file name, such as app.3f9a1c.js, which Vite and other bundlers do by default, and serve it with Cache-Control: public, max-age=31536000, immutable, so browsers and CDNs keep it for a year and never revalidate. When the file changes, its name changes, so users fetch the new one. Serve index.html with no-cache, so browsers always revalidate it and pick up the new file names after a deploy. Serve all of this from a CDN or Nginx, pre-compressed with Brotli and gzip.
| File | Header | Why |
|---|---|---|
assets/app.3f9a1c.js, .css, fonts | public, max-age=31536000, immutable | Name changes when the content does |
index.html | no-cache | Must point to the newest asset names |
favicon.ico, robots.txt | public, max-age=86400 | Unhashed; a day is fine |
| User uploads | public, max-age=604800 with unique names | Immutable once uploaded |
In Express, if you must serve them yourself:
ts
app.use('/assets', express.static('dist/assets', { immutable: true, maxAge: '1y' })) // (1)
app.get('*', (req, res) => {
res.set('Cache-Control', 'no-cache') // (2)
res.sendFile(path.resolve('dist/index.html'))
})- Hashed assets, cached for a year.
- The HTML shell, always revalidated. This is also the fallback for client-side routes in a single-page app (Vite deployment).
The deploy trap. Delete old hashed files too early and users with an old index.html open in a tab get 404 for the chunks it references. Keep the previous deploy's assets for a while.
18. How should a Node.js service be deployed behind Nginx?
The short answer
Run the Node app on a private port, such as 127.0.0.1:3000, or as several instances, under a process manager or containers. Configure Nginx to listen on 80 and 443, terminate TLS, redirect HTTP to HTTPS, serve static files directly, and proxy_pass everything else to an upstream of Node instances, passing Host, X-Forwarded-For and X-Forwarded-Proto. Add body size limits, timeouts, compression, keep-alive to the upstream, and the headers needed for WebSockets. In Express, set trust proxy. Firewall the Node port so only Nginx can reach it.
nginx
upstream orders_api {
server 127.0.0.1:3000;
server 127.0.0.1:3001;
keepalive 32; # (1)
}
server {
listen 443 ssl;
http2 on;
server_name api.shop.example;
ssl_certificate /etc/letsencrypt/live/api.shop.example/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/api.shop.example/privkey.pem;
client_max_body_size 10m; # (2)
gzip on;
gzip_types application/json text/css application/javascript;
location /assets/ {
root /srv/web; # (3)
expires 1y;
add_header Cache-Control "public, immutable";
}
location / {
proxy_pass http://orders_api;
proxy_http_version 1.1;
proxy_set_header Connection ""; # (4)
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_read_timeout 30s;
}
}
server {
listen 80;
server_name api.shop.example;
return 301 https://$host$request_uri; # (5)
}- Keep connections to Node open and reuse them, instead of a new TCP connection per request.
- Reject request bodies over 10 MB before they reach Node.
- Static files straight from disk.
- HTTP/1.1 with an empty
Connectionheader is what makes upstream keep-alive work. - Plain HTTP only redirects.
For WebSockets, add proxy_set_header Upgrade $http_upgrade; and proxy_set_header Connection "upgrade"; on the WebSocket location, and a longer proxy_read_timeout.
19. How do load balancers and reverse proxies differ?
The short answer
They overlap heavily, and one product often does both. A load balancer's main job is spreading traffic across many instances and removing unhealthy ones; it can work at layer 4, on raw TCP, or layer 7, on HTTP. A reverse proxy's main job is standing in front of servers and handling HTTP concerns on their behalf: TLS, caching, compression, routing, static files and security headers; it may front a single server. Nginx, HAProxy, Envoy and a cloud application load balancer are all reverse proxies that also load balance.
| Load balancer | Reverse proxy | |
|---|---|---|
| Main purpose | Distribute traffic, health checking | Act for the servers: TLS, caching, routing |
| Needs several backends | Yes, by definition | No |
| OSI layer | 4 or 7 | 7 |
| Caching and compression | Rarely | Often |
| Examples | AWS NLB, ALB, HAProxy | Nginx, Envoy, Caddy, a CDN |
A typical stack uses both: a cloud load balancer across zones, then Nginx or an ingress controller in front of the app pods, then the Node processes (Kubernetes ingress).