Sub topic 5
How a proxy relays HTTP and HTTPS traffic
This chapter assumes you know HTTP requests, methods and headers, what ports and DNS do, and that TLS encrypts a connection between two endpoints. It covers how a proxy forwards plain HTTP, how it handles HTTPS, and which headers keep the original client's details intact.
Relaying plain HTTP
A proxy terminates one TCP connection from the client and opens a second one to the next hop. Two connections exist, not one. The proxy reads the request from the first and writes a request on the second. Then it does the same for the response.
A client that is configured to use a forward proxy sends the full URL in the request line. This is called absolute form:
curl -x http://proxy.internal:3128 http://example.org/The request line becomes GET http://example.org/ HTTP/1.1. The proxy needs the full URL because the TCP connection goes to the proxy, not to the origin. A reverse proxy needs no such change. The client connects to the proxy's address believing it is the server, and sends an ordinary request.
The Host header decides which site a request is for. One reverse proxy often serves many domains from one IP address, so it matches Host against its server_name rules. When it forwards the request, it must choose whether to keep the original Host or replace it with the backend's name. Many applications build links from Host, so most setups preserve it.
Relaying HTTPS with CONNECT
A forward proxy cannot read an HTTPS request, because TLS encrypts it between the client and the origin. The client therefore asks the proxy for a tunnel with the CONNECT method:
CONNECT example.org:443 HTTP/1.1
Host: example.org:443
HTTP/1.1 200 Connection establishedAfter the 200 response, the proxy copies bytes in both directions without interpreting them. The TLS handshake runs end to end between client and origin. The proxy sees only the destination host and port, plus traffic volume and timing.
Reading HTTPS content requires a proxy that terminates TLS and starts a new TLS session to the origin. A reverse proxy does this with its own certificate. A forward proxy can only do it if clients trust a certificate authority that the proxy controls. A later chapter covers TLS termination.
Headers that carry the original client
After a proxy relays a request, the backend sees the proxy's IP address as the peer. To recover the real client, the proxy adds 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.comX-Forwarded-Forlists client addresses, with each proxy appending the address it received the request from. The leftmost entry is the original client claimed.X-Forwarded-Prototells the backend whether the client usedhttporhttps. This matters when the proxy terminates TLS and forwards plain HTTP.Forwardedis the standardized form of the same information.
Nginx sets these with proxy_set_header:
location / {
proxy_pass http://app_servers;
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_add_x_forwarded_for appends $remote_addr to any header the client already sent.
Hop-by-hop headers
Some headers describe a single connection, not the whole request path. These include Connection, Keep-Alive, Transfer-Encoding, TE, Upgrade and Proxy-Authorization. A proxy must remove them rather than pass them on. Each hop negotiates its own keep-alive and framing. Nginx also speaks HTTP/1.0 to backends by default and closes the connection after each request. Setting proxy_http_version 1.1 allows backend keep-alive.
Exercise
A client reaches https://example.org/ through a forward proxy using CONNECT. Can the proxy log the URL path /account? Why or why not?
Show answer
No. After the 200 response, the proxy only copies encrypted bytes, and the TLS session runs between the client and the origin. The proxy can log the destination host and port from the CONNECT line, but not the path, unless it terminates TLS with a certificate the client trusts.