Load Balancing Fundamentals Series

📡 Understanding the PROXY Protocol From a Top 1% Perspective — Why Does Everyone Look Like the Same IP Address Once a Load Balancer Is in the Way?

Introduction

This article is part of the Top 1% Series Complete Article Guide, and the eighth article in the Load Balancing Fundamentals Series.

Prerequisite Knowledge

Getting the Big Picture

Looking at a backend server’s access log behind a load balancer, you might run into a symptom where the client IP address that should have been recorded is instead replaced everywhere with the load balancer’s own IP address. This isn’t a misconfiguration — it’s an inevitable consequence of the load balancer’s own structure.

graph LR
    Client["Client<br/>(203.0.113.5)"] -->|"TCP Connection 1"| LB["Load Balancer<br/>(10.0.0.100)"]
    LB -->|"TCP Connection 2<br/>source: 10.0.0.100"| Backend["Backend Server"]

Deep Dive Into the Fundamentals

Why Does the Client’s IP Address Get Lost?

As covered in the L4/L7 distinction, an L7 load balancer establishes a separate TCP connection to the client and to the backend server. From the backend server’s point of view, the party it’s communicating with is always the load balancer itself — nothing about the TCP/IP connection information can tell it anything about the real client beyond it. The source IP address recorded in a backend server’s access log all being the load balancer’s own address is simply an inevitable consequence of this connection structure.

X-Forwarded-For: the Most Common Solution, Limited to L7

The X-Forwarded-For (XFF) header is a mechanism where an L7 load balancer appends a piece of information to the HTTP request it forwards to the backend server, saying “the real source was this IP address,” as an HTTP header. An application on the backend server side can read this header to learn the real client IP address.

X-Forwarded-For: 203.0.113.5

This approach has a structural limit. Because XFF is simply part of an application-layer HTTP header, it can only be used by a load balancer that can actually read and write HTTP content — an L7 feature. An L4 load balancer, which never looks at HTTP content at all, or a configuration like SSL passthrough where the load balancer can’t read HTTP content, has no room to append an XFF header at all.

What Happens to XFF Across Multiple Load Balancers or Proxies?

When multiple load balancers or reverse proxies sit in a chain between the client and the backend server, the XFF header accumulates a comma-separated IP address for each proxy it passed through.

X-Forwarded-For: 203.0.113.5, 198.51.100.10

Here, the most trustworthy “real client IP” is the first entry in the list (the one appended earliest). The further back a value sits in the list, the more likely it was appended by a proxy under your own control, raising its trustworthiness. On the flip side, if a malicious client sends a forged XFF header from the very start, that forged value risks slipping in as the list’s first entry — so deciding how far into the list you can trust requires accurately knowing how many proxies you yourself actually manage.

PROXY Protocol: a Lower-Layer Solution That Also Works at L4

The PROXY protocol takes a lower-layer approach: instead of an HTTP header, right after a TCP connection gets established, it inserts a header carrying the real source information, before the protocol’s own content even begins. Even an L4 load balancer, which never interprets HTTP content at all, can convey the client’s real IP address to a backend server this way.

ApproachOperates atCompatible Load Balancers
X-Forwarded-ForAn HTTP header (application layer)L7 load balancers only
PROXY protocolRight after a TCP connection (close to the transport layer)Either L4 or L7 load balancers

Using the PROXY protocol requires both the load balancer side and the backend server side to support the protocol. On the backend server side (nginx, or HAProxy itself, say), you have to explicitly enable the setting that interprets a PROXY protocol header attached to the front of an incoming connection — otherwise its content gets accidentally misinterpreted as part of the HTTP request, producing an error.

What a Pro Sees Here (Top 1% Understanding)

How Information Gets Conveyed Is Always Constrained by Which Layer It Rides On

The reason two solutions, XFF and the PROXY protocol, exist is simply a difference in approach — at which layer (the application layer, or a layer below it) do you convey the same single piece of information, “I want to tell you the client’s real IP address.” With this view in hand, the question “why can’t I use XFF with an L4 load balancer” becomes structurally explainable: “XFF is an HTTP header, and HTTP is application-layer information, so an L4 load balancer that never interprets it naturally can’t use it.” Nearly every “why does this work but not that” question about load balancers and proxies resolves once you step back to this view — which layer is the information actually riding on.

Common Misconceptions and Pitfalls

Troubleshooting Perspective

  1. The client’s IP address never shows up in a backend’s access log: Check whether the load balancer is configured to append the XFF header, or, for an L4 setup, whether the PROXY protocol is enabled.
  2. A communication error starts the instant the PROXY protocol is enabled: Check whether the backend server side has the setting that interprets a PROXY protocol header (like nginx’s proxy_protocol) actually enabled.
  3. The XFF header contains an untrustworthy value: Recount exactly how many proxies you yourself actually manage, and recheck how far into the list you should actually trust.

Summary

Takeaways to Apply Today

  1. When you run into a lost client IP, first check whether your setup is L4 or L7, and choose the appropriate fix (XFF or the PROXY protocol).
  2. When trusting an XFF header, accurately count how many proxies you yourself actually manage, and decide how far into the list you should trust.

References

Comments

Feel free to leave thoughts, questions, or corrections below (sign in with a GitHub account to comment or react).