Nginx Edge & Reverse Proxy
Production Nginx patterns—graceful reloads, upstream keepalive pools, proxy_pass trailing-slash rules, security headers, rate limiting, and 499/502/504 triage.
TL;DR
Nginx handles edge TLS termination, routing, security headers, and volumetric per-IP rate limiting—leave JWT/business auth to your API gateway so validation logic never drifts. Always test with nginx -t before nginx -s reload, configure upstream keepalive pools, and watch out for add_header inheritance and proxy_pass trailing slashes.
Operations
# Always validate before reloading (not run)
nginx -t # syntax + file check
nginx -T # dump full resolved config across includes
nginx -s reload # graceful reload: drains in-flight requestsPrefer nginx -s reload (or systemctl reload nginx) over restart. Reloading spawns new worker processes with the new config while letting old workers finish active connections.
Production proxy & keepalive pattern
# Upstream pool with keepalive (not run)
upstream api_backend {
least_conn;
server 10.20.1.11:8080 max_fails=3 fail_timeout=10s;
server 10.20.1.12:8080 max_fails=3 fail_timeout=10s;
keepalive 64;
}
limit_req_zone $binary_remote_addr zone=perip:10m rate=20r/s;
server {
listen 443 ssl;
http2 on;
server_tokens off;
# Always include `always` so headers apply on 4xx/5xx responses too
add_header Strict-Transport-Security "max-age=63072000; includeSubDomains" always;
add_header X-Content-Type-Options "nosniff" always;
limit_req zone=perip burst=40 nodelay;
limit_req_status 429;
location / {
proxy_pass http://api_backend;
proxy_http_version 1.1;
proxy_set_header Connection ""; # required for upstream keepalive
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_connect_timeout 3s;
proxy_read_timeout 30s;
# Disable retries for non-idempotent payment/write endpoints:
# proxy_next_upstream off;
}
}Gotchas
proxy_passtrailing slash:location /api/ { proxy_pass http://up/; }strips/api/, whereasproxy_pass http://up;preserves/api/.add_headerinheritance: Adding a singleadd_headerinside alocationblock silently drops alladd_headerdirectives from the parentserverblock. Put shared headers in anincludesnippet.- Trusted proxy IPs: Without
real_ip_module(set_real_ip_from), clients behind a load balancer share the LB's IP or can spoofX-Forwarded-For, breaking per-IP rate limits. - Caching authenticated responses: Never enable
proxy_cacheon endpoints that readAuthorizationorCookieunless the identity is explicitly part ofproxy_cache_key. - Snippet annotations in
ingress-nginx: Disableallow-snippet-annotationsin multi-tenant Kubernetes clusters so Ingress authors cannot inject arbitrary Nginx directives.
Production checklist
-
nginx -truns in CI and as an Ansiblevalidate:step before every reload. -
server_tokens off;, TLS 1.2/1.3 only, andalwayson security headers. - Upstream pools use
keepalive+proxy_http_version 1.1+Connection "". - Non-idempotent endpoints disable
proxy_next_upstreamretries to prevent double writes. - Structured JSON access logs include
$request_time,$upstream_response_time, and$request_id.
Check yourself
Check Yourself
1. You add `add_header Cache-Control "no-store" always;` inside `location /api/` when `Strict-Transport-Security` is defined in the parent `server` block. What happens on `/api/` responses?
2. Access logs show a spike in HTTP 499 status codes. What happened?
Sources
Related Notes
Ansible Essentials
Agentless host configuration and rolling VM deployments—inventory, variable precedence, idempotency, handlers, and safe rollouts.
API Gateway Choice: Kong vs Gravitee
Decide between Kong (programmable DB-less API gateway) and Gravitee (full API management with developer portal, plans, and subscriptions).