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
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.
At the edge, overwrite rather than append (proxy_set_header X-Forwarded-For $remote_addr;), or have the app take the rightmost
address added by a proxy it trusts. When Nginx itself is behind a load
balancer, use the realip module (set_real_ip_from <lb range>; real_ip_header X-Forwarded-For;) so $remote_addr becomes the real
client, and only for addresses you trust.
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:
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
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
- Send a spoofed
X-Forwarded-Forand see what the app receives. Change the config to overwrite it and try again. - Remove
proxy_set_header Connection "";, reload, run 1,000 requests, and count connections inTIME_WAITinside an app container (cat /proc/net/tcp | wc -lis enough to see the difference). - Call
/api/slow?ms=4000and/api/slow?ms=6000. Which one returns 504, after how long?