Nginx & reverse proxies · Lesson 4 of 6 · 3 min read
TLS, security and performance
TLS termination
server {
listen 443 ssl;
http2 on;
server_name shop.example.com;
ssl_certificate /etc/letsencrypt/live/shop.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/shop.example.com/privkey.pem;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_session_cache shared:SSL:10m;
add_header Strict-Transport-Security "max-age=31536000" always;
}
server {
listen 80;
server_name shop.example.com;
return 301 https://$host$request_uri;
}
- Use the full chain (
fullchain.pem). A missing intermediate certificate works in some browsers (they cache intermediates) and fails incurland mobile apps. ssl_session_cachelets returning clients resume sessions and skip a full handshake.- HSTS tells browsers to use HTTPS only. Start with a short
max-agebecause it can't be undone quickly. - Let's Encrypt with
certbot --nginxor awebrootchallenge renews every 60–90 days; reload Nginx after renewal (certbot's deploy hook does it). - To re-encrypt to the backend, use
proxy_pass https://...withproxy_ssl_verify on;andproxy_ssl_trusted_certificate, otherwise the upstream certificate isn't checked at all.
Rate limiting and access control
Nginx rate limiting is a leaky bucket per key:
http {
limit_req_zone $binary_remote_addr zone=login:10m rate=5r/m;
limit_req_status 429;
server {
location = /login {
limit_req zone=login burst=10 nodelay;
proxy_pass http://app;
}
}
}
rate=5r/mis the steady rate; Nginx tracks it in milliseconds, so10r/smeans one request every 100 ms, not ten at once.burst=10queues up to 10 extra requests; withoutnodelaythey are spaced out at the rate, withnodelaythey are served immediately but still use up the burst.- Requests beyond that are rejected with
limit_req_status(default 503; set 429). 10mof zone memory holds about 160,000 IP addresses.limit_conncaps concurrent connections per key, useful for downloads.
Enlarge diagram
Swipe sideways to see all of it, or tap to open it full screen.
Also: allow 10.0.0.0/8; deny all; for admin paths, auth_basic for quick protection, server_tokens off; to hide the version, and security headers (X-Content-Type-Options, Content-Security-Policy) with always so they're added to error responses too.
Caching and compression
proxy_cache_path /var/cache/nginx keys_zone=api:50m max_size=2g inactive=30m;
location /catalog/ {
proxy_cache api;
proxy_cache_valid 200 5m;
proxy_cache_use_stale error timeout updating;
proxy_cache_lock on;
add_header X-Cache-Status $upstream_cache_status;
proxy_pass http://catalog;
}
proxy_cache_use_staleserves an old copy when the backend is down or slow: a cheap resilience win.proxy_cache_locklets only one request refill an expired item, so a popular page expiring doesn't stampede the backend.X-Cache-Status(HIT,MISS,EXPIRED,STALE) makes caching visible when debugging.gzip on; gzip_types text/css application/json application/javascript;compresses text (HTML is included by default). Don't compress images or already-compressed formats.
The Caching & CDNs topic goes deeper into Cache-Control headers and edge caching.