Skip to Content
Mengty Learns
Platform tools

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.

referenceintermediateTested with nginx 1.30.8 / 1.31.10 (docs)Updated Nov 27, 2026

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 requests
Tip:

Prefer 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_pass trailing slash: location /api/ { proxy_pass http://up/; } strips /api/, whereas proxy_pass http://up; preserves /api/.
  • add_header inheritance: Adding a single add_header inside a location block silently drops all add_header directives from the parent server block. Put shared headers in an include snippet.
  • Trusted proxy IPs: Without real_ip_module (set_real_ip_from), clients behind a load balancer share the LB's IP or can spoof X-Forwarded-For, breaking per-IP rate limits.
  • Caching authenticated responses: Never enable proxy_cache on endpoints that read Authorization or Cookie unless the identity is explicitly part of proxy_cache_key.
  • Snippet annotations in ingress-nginx: Disable allow-snippet-annotations in multi-tenant Kubernetes clusters so Ingress authors cannot inject arbitrary Nginx directives.

Production checklist

  • nginx -t runs in CI and as an Ansible validate: step before every reload.
  • server_tokens off;, TLS 1.2/1.3 only, and always on security headers.
  • Upstream pools use keepalive + proxy_http_version 1.1 + Connection "".
  • Non-idempotent endpoints disable proxy_next_upstream retries 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