Sub topic 5
How a Proxy Relays HTTP and HTTPS Traffic
This chapter assumes you know the shape of an HTTP request and response (methods, status codes, headers), what a port and a DNS name are, and that TLS encrypts a connection and authenticates the server. It covers the mechanics that both forward and reverse proxies rely on. Later chapters build load balancing, TLS termination and caching on top of them.
Two connections, not one
A proxy that handles HTTP does not pass packets along untouched. It terminates one TCP connection from the client, reads the request, opens or reuses a second TCP connection to the next hop, and sends a request there. The response travels back the same way.
This has three consequences:
- The server sees the proxy's IP address as the peer, not the client's.
- The proxy can read, add, change or remove headers, because it parses HTTP.
- The two connections are independent. They can use different HTTP versions, different keep-alive behavior and different timeouts.
Plain HTTP through a forward proxy
A client that is configured to use a forward proxy sends the request to the proxy's address, not to the origin's. For plain HTTP, the request line contains the full URL (the absolute form), because the proxy needs to know where to send it:
GET http://example.org/index.html HTTP/1.1
Host: example.orgA request sent directly to an origin uses the origin form, GET /index.html HTTP/1.1, because the connection already identifies the server. The proxy reads the absolute URL, connects to example.org, and sends the origin form on that connection.
Because the proxy sees the whole request, it can apply URL-based rules and cache the response. This is what makes plain-HTTP proxying easy to filter and inspect.
The Host header and reverse proxies
A reverse proxy receives origin-form requests, since the client believes it is talking to the real site. The Host header tells the proxy which site the client wanted. This matters when one proxy fronts many sites.
By default, nginx rewrites Host to the name or address in proxy_pass when it contacts the backend. If the backend needs the original name, for example to pick a virtual host or to build absolute links, you must pass it on explicitly:
proxy_set_header Host $host;Forgetting this is a common source of backends that return the wrong site, redirect to an internal hostname, or generate links that point to 10.0.0.11:8080.
HTTPS through a forward proxy: CONNECT
For HTTPS, the client wants a TLS session with the origin. If the proxy had to read the request, it could not do that without breaking the encryption. So the client asks the proxy to open a tunnel with the CONNECT method. The request line names the destination host and port, not a path:
CONNECT example.org:443 HTTP/1.1
Host: example.org:443
HTTP/1.1 200 Connection establishedOnce the proxy replies with a 2xx status, it stops interpreting HTTP on that connection. It copies bytes in both directions between the client and the origin. The client then starts the TLS handshake with the origin through the tunnel.
You can watch this with curl:
curl -x http://proxy.internal:3128 https://example.org/Adding -v shows curl sending CONNECT example.org:443, receiving the 200, and only then starting the TLS handshake.
What the proxy can and cannot see
Inside a CONNECT tunnel the proxy sees:
- The destination host and port from the CONNECT line.
- The client IP address, timing, and the number of bytes transferred.
It does not see the URL path, query string, headers, cookies or body, because these are inside the TLS session. This is why a forward proxy can allow or deny HTTPS by domain and port, but not by path.
Some organizations inspect HTTPS by running a TLS-intercepting proxy. It presents a certificate it generates for each site, signed by a private certificate authority that is installed on managed clients. The proxy decrypts, inspects and re-encrypts the traffic. This is two separate TLS sessions, not a tunnel. It needs the client to trust the proxy's authority, and it has privacy and security costs. Treat it as a deliberate policy decision, not a default.
Restrict CONNECT
An open CONNECT is a way to reach any TCP service. A forward proxy that allows CONNECT to any port lets a client use it to reach SSH, SMTP or internal services. Real configurations restrict it, usually to port 443. Squid, for example, ships with an ACL that denies CONNECT to ports other than 443.
Forwarding headers
Because the proxy opens its own connection, the backend loses information about the original request. Reverse proxies restore it with headers.
X-Forwarded-For: 203.0.113.7, 10.0.0.5
X-Forwarded-Proto: https
Forwarded: for=203.0.113.7;proto=https;host=example.com| Header | Meaning |
|---|---|
| `X-Forwarded-For` | A comma-separated list of addresses. Each proxy appends the address of the peer it received the request from. |
| `X-Forwarded-Proto` | The scheme the client used, `http` or `https`. |
| `X-Forwarded-Host` | The `Host` value the client sent. |
| `Forwarded` | The standardized form (RFC 7239) that carries `for`, `proto`, `host` and `by` in one header. |
| `X-Real-IP` | A non-standard single-address header that some setups use. |
In the first example, 203.0.113.7 is the client, and 10.0.0.5 is an earlier proxy. The final proxy's own address is the TCP peer address, so it does not appear in the list.
In nginx, $proxy_add_x_forwarded_for appends the peer address to any existing header:
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;Do not trust these headers blindly
Any client can send its own X-Forwarded-For: 1.2.3.4. If the proxy appends to it, the leftmost entry is attacker-controlled. The reliable way to read the client address is to count from the right:
- Know how many trusted proxies sit in front of your application.
- Start from the TCP peer address, then walk the list from the right, skipping addresses that belong to your own trusted proxies.
- The first address that is not a trusted proxy is the client.
At the outermost proxy, the safest policy is often to overwrite the header with $remote_addr instead of appending. Do this when no trusted proxy sits in front of it.
Hop-by-hop headers and connection handling
Some headers apply to a single connection and must not be forwarded. These include Connection, Keep-Alive, Transfer-Encoding, TE, Upgrade and Proxy-Authorization. A proxy removes them, or handles them on each side separately.
Practical effects:
- Keep-alive. The client-side connection can stay open for many requests while the proxy-to-backend connection is closed after each request, or the reverse. nginx talks HTTP/1.0 to backends by default, which closes the connection after each request. To reuse backend connections, set
proxy_http_version 1.1;and clear theConnectionheader withproxy_set_header Connection "";, and add akeepalivedirective to the upstream. - Protocol versions. A client may use HTTP/2 to the proxy while the proxy uses HTTP/1.1 to the backend. Proxies translate between them.
- Timeouts. The client-side timeout and the backend-side timeout are separate settings. A slow backend can produce a
504 Gateway Timeoutfrom the proxy even though the client's connection is healthy. - Buffering. nginx buffers the backend response by default, which frees the backend quickly even when the client is slow. For streaming responses, such as server-sent events, buffering delays data, and you turn it off with
proxy_buffering off;.
WebSocket and upgrades
Upgrade is hop-by-hop, so a proxy drops it unless told otherwise. To proxy WebSocket you pass it explicitly:
location /ws/ {
proxy_pass http://app_servers;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}After the 101 Switching Protocols response, the proxy relays bytes in both directions, much like a CONNECT tunnel.
Errors a proxy produces
The proxy can answer the client by itself, and the status code tells you which hop failed.
| Status | Typical cause |
|---|---|
| `502 Bad Gateway` | The backend closed the connection, sent an invalid response, or refused the connection. |
| `503 Service Unavailable` | No backend is available, or the proxy is overloaded or rate limiting. |
| `504 Gateway Timeout` | The backend did not answer within the proxy's timeout. |
| `407 Proxy Authentication Required` | A forward proxy needs credentials, sent in `Proxy-Authorization`. |
Note the difference between 407 and 401. The 401 status comes from the origin and uses Authorization. The 407 status comes from the proxy and uses Proxy-Authenticate and Proxy-Authorization.
Common mistakes
- Setting
proxy_passto a hostname that nginx resolves only at startup. If the backend's DNS record changes, nginx keeps the old address until reload. - Dropping
X-Forwarded-Proto, so an application behind a TLS-terminating proxy thinks requests are plain HTTP and redirects forever. - Allowing CONNECT to all ports on a forward proxy.
- Trusting the first entry of
X-Forwarded-For. - Forgetting that the proxy hides the client address from backend access logs unless the header is parsed or logged.
Exercise
A client sits behind a forward proxy and requests https://example.org/account?id=42. The proxy log shows only CONNECT example.org:443. Why is the path missing, and which kind of rule can the proxy still enforce for this request?
Show answer
After the proxy returns 200 to CONNECT, it only relays bytes, and the TLS session between the client and example.org hides the path, query, headers and body. The proxy can still enforce rules based on the destination host and port (and the client address), such as allowing example.org:443 and denying other domains or ports. Rules on the path need TLS interception.
Exercise
A backend receives X-Forwarded-For: 198.51.100.9, 203.0.113.7 from your single trusted reverse proxy at 10.0.0.5, and the proxy appends the peer address it saw. The proxy's TCP peer address at the backend is 10.0.0.5. Which address is the real client, and why is the leftmost entry unsafe?
Show answer
The proxy appended the address it received the request from, so the rightmost entry, 203.0.113.7, is the client as the proxy saw it. The entry 198.51.100.9 was already in the header when the request arrived, so the client (or an earlier untrusted hop) supplied it and it can be forged.