How-To Guide

How to Send and Trust the Proxy Protocol Header

Preserve the real client address through a load balancer with the PROXY protocol header, and configure the proxy to trust it only from the balancer.

On this page

  1. Introduction
  2. Why the Backend Sees the Wrong Address
  3. The PROXY Protocol Header
  4. Enabling It on Both Ends
  5. Trust Only the Balancer
  6. Versions, IPv6 and TLVs
  7. Interaction with Other Headers
  8. Common Pitfalls and Conclusion

Introduction

When a load balancer terminates a connection and opens a new one to the backend, the backend sees the balancer’s address as the client, not the real user’s. HTTP headers such as X-Forwarded-For can carry the original address, but they are application-level and can be spoofed by anyone who can reach the backend directly. The PROXY protocol conveys the real client and server addresses at the transport level, before any application data, so the backend knows the true source from the moment the connection opens.

Why the Backend Sees the Wrong Address

A proxy or load balancer accepts the client’s connection and makes its own connection to the backend. At the transport level, the backend’s peer is the proxy, so any code that logs or authorises by the socket address sees the proxy, not the user. Access logs show one address, rate limiting throttles the balancer, and IP-based rules do not work.

Application-level forwarding headers patch this up, but they are part of the request body and can be forged by a client that reaches the backend directly. If the backend trusts them unconditionally, an attacker sets X-Forwarded-For to any address and spoofs the client. The PROXY protocol moves the information below the application, where a direct client cannot forge it.

# the problem
client -> [LB] -> backend
at the backend, peer address = LB, not client
# application headers (X-Forwarded-For) are forgeable if the backend is reachable
# PROXY protocol: a transport-level preamble with the real client address

The PROXY Protocol Header

The PROXY protocol prepends a small header to a connection carrying the original source and destination addresses. Version 1 is a single human-readable line; version 2 is a binary header with more detail and support for IPv6 and extra TLVs. The proxy sends it immediately after the connection opens, before any application bytes, and the backend parses it before treating the rest as the protocol.

Because it is at the connection level, it is independent of the application protocol: HTTP, HTTPS, gRPC and arbitrary TCP can all carry it. The backend must be configured to expect it, or it will interpret the header bytes as application data.

# PROXY protocol v1 (one line, before the payload)
PROXY TCP4 203.0.113.7 198.51.100.10 51234 443\r\n
<the real HTTP/gRPC/... stream follows>

# v2: binary header with addresses + optional TLVs (IPv6, SSL info, etc.)
# sent by the proxy, parsed by the backend BEFORE the application protocol

Enabling It on Both Ends

The proxy must be configured to send the header, and the backend to accept it, and both must agree on the version. In HAProxy, send-proxy enables it per backend server; in NGINX, proxy_protocol on the listen directive makes the server expect it and $proxy_protocol_addr gives the real client. A mismatch, one sending and the other not expecting, breaks the connection confusingly.

Enable it only on the listener that faces the balancer. A listener that also accepts direct client connections must not require the header, or direct clients (which do not send it) fail. Use separate listeners or ports for proxied and direct traffic.

# nginx: expect the PROXY header from the load balancer
server {
  listen 8080 proxy_protocol;            # this listener REQUIRES the header
  real_ip_header proxy_protocol;
  set_real_ip_from 10.0.0.0/8;          # trust only the LB's network
  location / {
    proxy_set_header X-Real-IP $proxy_protocol_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_pass http://app;
  }
}

Trust Only the Balancer

The security of the PROXY protocol depends on who can reach the listener. If the backend’s proxied listener is reachable directly, a client can send a forged header claiming any address. Restrict the listener to the load balancer’s network, at the firewall and in the application’s trusted-proxy configuration.

Set the real-IP trust range to the balancer’s address range, so even if a request arrives from elsewhere on the network, its header is not trusted. This is the same discipline as trusting a forwarding header only from the proxy: the trust must be anchored to a network boundary, not to the header’s presence.

# trust model
[ ] the proxy-protocol listener is reachable ONLY from the LB network
[ ] set_real_ip_from = the LB's address range (not 0.0.0.0/0)
[ ] direct-client listeners separate and do NOT require the header
# a header is trusted because of WHO sent it, not because it is present

Versions, IPv6 and TLVs

Version 2 handles IPv6 and carries optional TLVs such as the SSL information (whether the client TLS’d, the cipher, the SNI) or a unique connection id. If you need the original SNI or TLS details at the backend, version 2’s TLVs carry them; version 1 only carries addresses and ports.

Match the version on both ends, because a v1-sending proxy and a v2-expecting backend do not interoperate. Where both ends support it, v2 is preferred for IPv6 and the extra metadata, and most proxies let you choose the version.

# v1: PROXY TCP4 <src> <dst> <sport> <dport>
# v2: binary; supports IPv6 and TLVs such as:
#   PP2_TYPE_SSL       (client TLS info, SNI, cipher)
#   PP2_TYPE_UNIQUE_ID (a connection identifier)
# both ends must agree on the version; v2 for IPv6 + metadata

Interaction with Other Headers

The PROXY protocol gives the real client address; you still need to set the application-level headers (X-Forwarded-For, X-Real-IP) that downstream code and frameworks read. Populate them from the trusted proxy-protocol address, not from the incoming header values, so a client cannot inject a false chain.

Preserve the existing chain correctly: appending to X-Forwarded-For must use the trusted client address, and the backend must read the chain from the right end (the address the trusted proxy added), not from the left (which the client controls). Getting this wrong is the classic header-spoofing bug.

# set app headers FROM the trusted proxy address
X-Real-IP       = $proxy_protocol_addr           (the real client)
X-Forwarded-For = $proxy_protocol_addr + existing chain (from the trust boundary)
# the backend reads the client address the TRUSTED proxy appended,
# never the leftmost value a client could have supplied

Common Pitfalls and Conclusion

Do not require the header on a listener that also accepts direct clients. Do not trust the header without restricting who can reach the listener. Do not mismatch versions between the sender and the receiver. Do not keep trusting an application header while adding the proxy protocol; derive the app headers from the trusted address. And do not forget IPv6, which needs version 2.

The PROXY protocol carries the real client address at the transport level, below the application, so a direct client cannot forge it. Enable it on the proxy and the backend with matching versions, restrict the proxied listener to the balancer’s network, set real-IP trust to the balancer’s range, and derive application headers from the trusted address. Then logs, rate limits and IP rules see the real user.

Verify the address end to end by checking that the backend logs the real client IP.

Sources: HAProxy: PROXY protocol, NGINX: proxy_protocol, HAProxy: send-proxy.

FAQ

Is the PROXY protocol header forgeable?
Only if a client can reach the listener directly. The security is the network boundary: a client that can connect to the proxied port can send a forged header claiming any address. Restrict that listener to the load balancer's network and set the real-IP trust range accordingly.
Why not just use X-Forwarded-For?
Because it is an application-level header anyone reaching the backend can set. The PROXY protocol conveys the address below the application, so a direct client cannot forge it; it is also protocol-agnostic, working for HTTP, gRPC and raw TCP alike.

Related Examples