Learn Labs
Serving & Proxying

Reverse Proxy

proxy_pass, forwarded headers, keepalive, buffering, and timeouts

A reverse proxy accepts a client's request, makes its own request to a backend, and relays the answer. The backend talks only to Nginx; the client talks only to Nginx. Everything the backend knows about the client, it learns from headers Nginx adds.

The API location

location /api/ {proxy_pass http://app;the upstream groupproxy_http_version 1.1;proxy_set_header Connection "";allow keepalive to backendsproxy_set_header Host $host;the name the client usedproxy_set_header X-Real-IP $remote_addr;the client's addressproxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header X-Forwarded-Proto $scheme;https, so the app builds https linksproxy_set_header X-Request-ID $request_id;correlate Nginx and app logs}

What the backend sees:

curl --cacert ca.crt https://localhost:8443/api/whoami
{"instance": "app-3", "peer": "172.30.0.5",            # the TCP peer is Nginx
"host": "localhost",
"x_real_ip": "172.30.0.1",                            # the client (here: the Docker gateway = the host)
"x_forwarded_for": "172.30.0.1",
"x_forwarded_proto": "https",
"x_request_id": "783d363654c799aed9f47e4a56141ff9"}

Without these headers, every request would appear to come from Nginx: rate limits by IP, audit logs and geolocation in the app would all be wrong, and an app behind TLS termination would think it's on plain HTTP and generate http:// redirects.

Don't trust what clients send

curl --cacert ca.crt -H 'X-Forwarded-For: 6.6.6.6' -H 'X-Real-IP: 7.7.7.7' \
https://localhost:8443/api/whoami
"x_real_ip": "172.30.0.1",                  # overwritten by Nginx: safe
"x_forwarded_for": "6.6.6.6, 172.30.0.1",   # appended to: the client's fake value survives

$proxy_add_x_forwarded_for appends to whatever the client sent. That's correct when Nginx sits behind another trusted proxy, and a hole when it's the edge: an app that takes the first address in X-Forwarded-For believes 6.6.6.6.

Keepalive to backends

By default Nginx opens a new TCP connection to the backend for every request and closes it afterwards: a handshake each time, and a pile of sockets in TIME_WAIT under load. Upstream keepalive needs three things together:

upstream app {...keepalive 32;idle connections kept per worker}location /api/ {proxy_http_version 1.1;1.0 (the default) has no keepaliveproxy_set_header Connection "";don't forward the client's "Connection: close"}

Buffering

By default Nginx buffers the backend's response: it reads it as fast as the backend can send, then feeds it to the client at the client's pace. A slow mobile client then ties up a cheap Nginx connection, not an expensive app worker. Turn it off (proxy_buffering off) only for streaming responses such as server-sent events.

Request bodies are buffered too (proxy_request_buffering on, the default): a slow upload reaches the app all at once, after Nginx has it.

Timeouts

server {proxy_connect_timeout 2s;backend not accepting connectionsproxy_read_timeout 5s;max silence between two reads from the backend -> 504proxy_send_timeout 5s;max silence while sending the request}

The defaults are 60 seconds each. A connect timeout of 60 s means a dead backend costs every unlucky client a minute. Set them at server level (see the inheritance trap) and override per location where a route is known to be slow.

proxy_read_timeout is the time between two successive reads, not the whole response: a backend trickling a byte every 4 seconds never times out.

Lab

  1. Send a spoofed X-Forwarded-For and see what the app receives. Change the config to overwrite it and try again.
  2. Remove proxy_set_header Connection "";, reload, run 1,000 requests, and count connections in TIME_WAIT inside an app container (cat /proc/net/tcp | wc -l is enough to see the difference).
  3. Call /api/slow?ms=4000 and /api/slow?ms=6000. Which one returns 504, after how long?

On this page