Skip to content

Setting up Nginx as a reverse proxy in front of an application server

How to put Nginx in front of Node, Python, PHP-FPM, or any HTTP upstream — proxy_pass, the headers that matter, buffering trade-offs, and the timeouts that prevent silent 504s.

Nginx in front of an application server is the standard production layout: TLS terminates at Nginx, static files are served straight from disk, and Nginx forwards the dynamic requests to your app over HTTP on localhost. Done right, it is the single most useful piece of glue in a web stack. Done wrong, the app sees 127.0.0.1 as every client IP and your security tooling is blind.

How to verify

Confirm what is in front of what:

ss -ltnp | grep -E ':80|:443|:3000|:8080'
sudo nginx -T | grep -E 'proxy_pass|server_name'
curl -I http://127.0.0.1/
curl -I http://127.0.0.1:3000/      # direct hit on the app

If the upstream and Nginx return different headers (e.g. Server: nginx on one and Server: gunicorn on the other), you have visibility into both. The proxy_pass URL inside the Nginx config tells you what is upstream of which route.

What’s happening

proxy_pass http://127.0.0.1:3000 tells Nginx to forward the request to the upstream. By default, Nginx rewrites a few things in flight: it consumes the request body (buffering it to disk if large), sets Host to the value of $proxy_host (the IP), and drops the Upgrade header. Each of those defaults is wrong for at least one common case, which is why a reverse-proxy block always has a handful of explicit proxy_set_header lines.

The application sees a connection from 127.0.0.1. For analytics, rate limiting, and abuse blocking to work, the proxy has to pass the original client IP and the application must read it from the right header. The two headers in play are X-Real-IP (the immediate client only) and X-Forwarded-For (a chain). Pick one convention and stick to it.

The procedure

  1. Block scaffold inside a server block:
    location / {
        proxy_pass         http://127.0.0.1:3000;
        proxy_http_version 1.1;
    
        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_set_header X-Forwarded-Host  $host;
    
        proxy_connect_timeout 5s;
        proxy_send_timeout    60s;
        proxy_read_timeout    60s;
    }
  2. Decide on buffering. For most web apps, default buffering (request and response) is correct. For SSE/streaming endpoints or anything that emits chunks over time, disable it per-location:
    location /stream {
        proxy_pass http://127.0.0.1:3000;
        proxy_buffering off;
        proxy_cache off;
    }
  3. Make the app trust the forwarded headers. For Node/Express: app.set('trust proxy', 'loopback'). For Django: SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https'). For PHP behind Nginx, read $_SERVER['HTTP_X_FORWARDED_FOR'] and trust only your proxy IPs.
  4. If the upstream is on a unix socket (faster than TCP for same-host, sidesteps the loopback TCP stack), use it directly: proxy_pass http://unix:/run/myapp/app.sock;.
  5. Set proxy_pass_header for any header the upstream emits that you want untouched — e.g. Server if the upstream is signing it.
  6. Reload with sudo nginx -t && sudo systemctl reload nginx. Re-hit with curl -H 'X-Real-Test: 1' http://your-domain/ and check the app’s log shows the request, the right client IP, and the right scheme.
  7. Tune timeouts to the workload. A reporting endpoint that legitimately takes 90s needs proxy_read_timeout 120s on its own location — do not raise the global timeout to make a slow route work.

Common pitfalls

  • Forgetting proxy_set_header Host $host; — Nginx defaults to the upstream IP, and apps that key on Host (multi-tenant SaaS, OAuth callbacks) break.
  • Sending X-Forwarded-Proto: https to an upstream that does a 301 to https when it sees http — the upstream now ignores the redirect logic because Nginx claims the request was already TLS.
  • proxy_pass http://upstream/ versus proxy_pass http://upstream — the trailing slash decides whether /foo/bar becomes /bar or stays /foo/bar. Read Nginx’s docs once and pick the version you mean.
  • Letting client_max_body_size default to 1 MiB on an upload endpoint. The upload returns 413 before the upstream ever sees the request.
  • Buffering on a long-poll/SSE endpoint — the client sees no data until the upstream closes the connection.

In the engagements we run, the reverse-proxy block is the seam where most production incidents land — a Host mishandle here, a missing trust-proxy there. Stack Harbor maintains the proxy configuration as code (see Nginx config test and reload) and operates the upstream-Nginx pair together as part of managed operations.