The PROXY protocol is a lightweight header format that a proxy or load balancer sends at the very start of a forwarded TCP connection to tell the backend server the original client IP address and port. In load balancing, it solves a basic problem of relayed traffic: once an intermediary opens its own connection to the server, the server sees only the proxy’s address, not the address of the real client.
The protocol was designed by the HAProxy project and is now supported by most major proxies, cloud load balancers, web servers and mail servers. It works below the application layer, so it carries client information for any TCP-based protocol, including encrypted traffic that the proxy never decrypts.
How the PROXY protocol header works
The PROXY protocol works by inserting one header before any application data on a new connection. The sending proxy accepts the client connection, opens a separate connection to the backend, writes the header describing the original endpoints, and then relays the client bytes unchanged. The receiving server reads and strips the header before passing the remaining stream to its normal protocol handler.
The header is sent exactly once per connection, not once per request. This makes it well suited to Layer 4 load balancing, where the proxy forwards a byte stream without understanding it, and it keeps the overhead to a few dozen bytes per connection regardless of how much data follows.
Both sides must agree in advance. A server configured to expect the header will reject connections that lack it, and a server that does not expect it will treat the header as garbage application data. The protocol has no negotiation step, so the setting is a static, explicit configuration on each listener.
The following table lists the fields that a PROXY protocol header conveys to the backend.
| Field | Meaning | Example value |
|---|---|---|
| Address family | IPv4, IPv6 or Unix socket | TCP4 |
| Source address | Original client IP | 203.0.113.25 |
| Destination address | Address the client connected to | 198.51.100.10 |
| Source port | Client ephemeral port | 51234 |
| Destination port | Listener port on the proxy | 443 |
Version 1 and version 2 of the PROXY protocol
The PROXY protocol exists in two versions: a human-readable text format (version 1) and a compact binary format (version 2). Version 1 is a single ASCII line such as PROXY TCP4 203.0.113.25 198.51.100.10 51234 443 followed by a carriage return and line feed, with a maximum length of 107 bytes.
Version 2 begins with a fixed 12-byte signature that cannot be confused with ordinary protocol traffic, followed by a version and command byte, a family byte, a length field and the address block. Its main advantage is extensibility through TLV fields (type-length-value), which can carry extra metadata such as the TLS version, the client certificate status, the negotiated ALPN protocol or a cloud-specific endpoint identifier.
This comparison summarizes the practical differences between the two versions.
| Property | Version 1 | Version 2 |
|---|---|---|
| Encoding | ASCII text line | Binary structure |
| Maximum header size | 107 bytes | Variable, usually under 300 bytes |
| Parsing effort | String parsing | Fixed offsets, faster |
| Extra metadata | Not supported | TLV extensions |
| Health check marker | Only UNKNOWN family | LOCAL command |
| Typical use today | Legacy and debugging | Default in new deployments |
The version 2 LOCAL command tells the receiver that the connection was opened by the proxy itself, for example for a health check, so the server should use the real connection addresses. Most modern software accepts both versions, but each listener should be set explicitly to avoid mismatches.
PROXY protocol compared with other ways to preserve the client address
The PROXY protocol is one of several methods for passing the client address through a load balancer, and the right choice depends on the layer at which the balancer operates. In Layer 7 load balancing, where the proxy terminates HTTP, the common alternative is the X-Forwarded-For header or the standardized Forwarded header defined in RFC 7239.
Other designs avoid rewriting the source address altogether. Direct server return and transparent proxying keep the original client IP on the packets, but they require routing changes, special kernel features or shared virtual addresses on the servers.
| Method | Layer | Works with encrypted traffic | Main limitation |
|---|---|---|---|
| PROXY protocol | Layer 4 | Yes, without decryption | Both ends must support it |
| X-Forwarded-For header | Layer 7 (HTTP) | Only after TLS termination | HTTP only, easily spoofed |
| Transparent proxying | Layer 3 and 4 | Yes | Needs routing and kernel setup |
| Direct server return | Layer 2 or 3 | Yes | Return path bypasses the balancer |
The key distinction is that the PROXY protocol is protocol-agnostic: it serves SMTP, IMAP, database connections, MQTT and passthrough HTTPS equally well, whereas HTTP headers exist only where the balancer understands HTTP.
Configuring the PROXY protocol in practice
A working setup requires matching configuration on both hops: the sender must be told to emit the header and the receiver must be told to expect it. In HAProxy this is the send-proxy or send-proxy-v2 option on a server line, while NGINX uses proxy_protocol on the listen directive and Envoy Proxy uses a dedicated listener filter.
The receiver should trust the header only from known proxies. Software such as NGINX lets administrators restrict which source networks may supply client addresses, and firewall rules should block direct access to the backend port so that no outside client can forge a header.
The table below gives typical deployment situations and the recommended configuration.
| Situation | Recommended approach | Reason |
|---|---|---|
| TLS passthrough to web servers | Send version 2 from the balancer | Client IP without decryption |
| Mail servers behind a balancer | Enable it on SMTP and IMAP listeners | Spam filters need the real IP |
| Chained proxies | Accept and resend at each hop | Preserves the first client address |
| HTTP balancer that terminates TLS | Prefer forwarding headers | Simpler for HTTP applications |
| Health checks from the balancer | Use the LOCAL command | Avoids false client entries |
Security risks and common mistakes
The most serious risk is header spoofing. If a backend accepts PROXY protocol headers from any source, an attacker who can reach it directly can claim any client address, which defeats IP-based access control, rate limiting and audit logging.
- Mismatched configuration produces immediate failures: browsers show protocol errors, and logs show unreadable request lines beginning with the PROXY signature.
- Enabling it on only some listeners behind the same balancer causes intermittent errors that are hard to trace.
- Forgetting health checks leads to failed probes when the server expects a header that the checker does not send.
- Assuming it encrypts anything is wrong: the header itself travels in clear text before any TLS handshake.
- Losing the header at an intermediate hop silently replaces the client address with an internal proxy address.
In plain words
When a load balancer passes your connection to a server, the server normally thinks the load balancer is the visitor. The PROXY protocol fixes this with a short note attached to the front of the connection. The note says who really connected and from where.
Think of a receptionist who forwards a phone call and first says, “This caller is from extension 25.” The server reads that note, removes it, and then handles the conversation as usual. The main point is simple: the server learns the real visitor address, but it should only believe notes that come from its own trusted receptionist.
A real-world example
An email hosting provider runs four mail servers behind a pair of TCP load balancers. Before the change, every incoming SMTP connection appeared to come from the balancer address 10.0.0.5, so the spam filter could not check sender reputation and the logs were useless for abuse reports.
The team enabled send-proxy-v2 on the balancers and turned on PROXY protocol support on the SMTP listener on ports 25 and 587, accepting headers only from the two balancer addresses. A firewall rule blocked direct access to the mail servers from the internet.
After the change, the spam filter received the real sender IP addresses again, blocklist checks resumed, and the share of spam reaching user inboxes dropped noticeably within a week. The TLS sessions remained end-to-end between clients and mail servers, because the balancers never had to decrypt any traffic.