PROXY protocol

On this page 7

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.

FieldMeaningExample value
Address familyIPv4, IPv6 or Unix socketTCP4
Source addressOriginal client IP203.0.113.25
Destination addressAddress the client connected to198.51.100.10
Source portClient ephemeral port51234
Destination portListener port on the proxy443
Information carried in a PROXY protocol header

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.

PropertyVersion 1Version 2
EncodingASCII text lineBinary structure
Maximum header size107 bytesVariable, usually under 300 bytes
Parsing effortString parsingFixed offsets, faster
Extra metadataNot supportedTLV extensions
Health check markerOnly UNKNOWN familyLOCAL command
Typical use todayLegacy and debuggingDefault in new deployments
Comparison of PROXY protocol versions

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.

MethodLayerWorks with encrypted trafficMain limitation
PROXY protocolLayer 4Yes, without decryptionBoth ends must support it
X-Forwarded-For headerLayer 7 (HTTP)Only after TLS terminationHTTP only, easily spoofed
Transparent proxyingLayer 3 and 4YesNeeds routing and kernel setup
Direct server returnLayer 2 or 3YesReturn path bypasses the balancer
Methods for preserving the original client address

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.

SituationRecommended approachReason
TLS passthrough to web serversSend version 2 from the balancerClient IP without decryption
Mail servers behind a balancerEnable it on SMTP and IMAP listenersSpam filters need the real IP
Chained proxiesAccept and resend at each hopPreserves the first client address
HTTP balancer that terminates TLSPrefer forwarding headersSimpler for HTTP applications
Health checks from the balancerUse the LOCAL commandAvoids false client entries
Typical PROXY protocol deployment choices

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.