How to Configure Proxy Protocol with HAProxy and Nginx on Linux: Preserving Real Client IPs Behind a Layer 4 Load Balancer

Network tutorial - IT technology blog
Network tutorial - IT technology blog

You set up a Load Balancer in front of your web server, open the Nginx access log, and get an unpleasant surprise: every single request shows the exact same IP address—the Load Balancer’s internal IP. The fallout is immediate. Rate limiting blocks your entire site, GeoIP reports the wrong locations, and troubleshooting incidents becomes nearly impossible.

I learned this the hard way when running an e-commerce web cluster handling around 3,000 requests per second. At the time, the system was dropping packets randomly during peak hours from 8:00 PM to 10:00 PM. HAProxy was running at Layer 4, forwarding traffic straight to the Nginx cluster behind it.

When I opened the logs to track down which client was spamming retries, the entire log file showed nothing but HAProxy’s IP: 10.0.0.10. After spending nearly a week struggling to isolate the source of the bad traffic, I decided to switch to Proxy Protocol. After 6 months of stable production use, I’m sharing my real-world takeaways along with a configuration guide so you don’t have to waste time on trial and error.

1. Comparing 3 Ways to Preserve Real IPs Behind a Load Balancer

To let Nginx see the user’s original IP address when traffic passes through HAProxy, there are 3 main approaches. Each operates at a different network layer:

  • Option 1: Layer 7 HTTP + X-Forwarded-For Header — HAProxy runs in mode http, terminates TLS at the Load Balancer, inspects the HTTP request, and injects an X-Forwarded-For or X-Real-IP header before forwarding it to Nginx.
  • Option 2: Direct Server Return (DSR) / TPROXY — Uses Layer 3/4 routing techniques combined with iptables and the Linux kernel to preserve the client’s original source IP in the IP packet itself.
  • Option 3: Proxy Protocol (v1/v2) at Layer 4 — HAProxy keeps the connection in mode tcp (without decrypting HTTP/HTTPS). It simply prepends a tiny header to the start of the TCP stream to tell Nginx the client’s original IP and port.

The biggest differences come down to TLS handling and operational overhead. Layer 7 requires the Load Balancer to hold your SSL certificates. DSR requires deep changes to the OS routing tables. Proxy Protocol sits neatly in the middle: lightweight while still preserving end-to-end encrypted connections.

2. Real-World Assessment After 6 Months in Production

Layer 7 + X-Forwarded-For

Pros: Easy to understand and compatible with almost every web server and backend application. You can route traffic by URL path (for example, /api to the backend cluster and /static to an image server) right inside HAProxy.

Cons: HAProxy has to decrypt SSL/TLS. When traffic spikes to several thousand concurrent connections, the Load Balancer’s CPU spends an extra 15–25% just handling TLS handshakes. Additionally, this approach won’t work if you need to load balance raw TCP protocols like databases or MQTT.

DSR / TPROXY on Linux

Pros: Delivers the highest performance. Return packets go straight from the backend to the client without looping back through HAProxy, while preserving the source IP at the kernel level.

Cons: Complex to configure and picky about infrastructure. Many Cloud VPC providers (such as AWS or GCP) block DSR-style ARP/routing by default. Whenever the network gets flaky, tracing packets with tcpdump is quite painful.

Proxy Protocol (v1 and v2)

Pros: This is the option I’m happiest with for a TLS Passthrough setup. HAProxy simply forwards the raw TCP stream down to Nginx. SSL certificates stay centralized on Nginx, making automatic renewal via Certbot much cleaner. Proxy Protocol has two versions: v1 uses plain text (up to 108 bytes, easy to read when debugging) and v2 uses a binary format (only 28 bytes for IPv4, much faster to parse in production).

Cons: Both ends must match in configuration. If HAProxy sends the Proxy Protocol header before Nginx is configured to accept it, connections will drop immediately—Nginx will mistake that header for corrupted HTTP/TLS data.

3. When Should You Choose Each Solution?

Depending on your infrastructure and requirements, you can make a quick call based on these 3 scenarios:

  • Choose Layer 7 + X-Forwarded-For if you want centralized SSL management on the Load Balancer, need to modify HTTP headers, or want HAProxy to route traffic by URL.
  • Choose DSR / TPROXY if you have full control over bare-metal network gear (routers/switches) and handle massive bandwidth such as video streaming or game servers.
  • Choose Proxy Protocol v2 if you want to run HAProxy at Layer 4 (mode tcp), keep TLS Passthrough so Nginx handles SSL itself, or run other TCP services while still needing real client IPs.

For typical web workloads on a Linux VPS or Cloud VM, Proxy Protocol v2 is usually the sweet spot: low CPU overhead on the Load Balancer, full TLS Passthrough, and easy long-term maintenance.

4. Step-by-Step: Configuring Proxy Protocol with HAProxy and Nginx

Suppose our lab setup consists of 2 servers:

  • HAProxy Load Balancer: 10.0.0.10 (listening on port 443)
  • Nginx Backend Server: 10.0.0.20 (listening on port 443)

Step 1: Enable Proxy Protocol v2 on HAProxy

Open /etc/haproxy/haproxy.cfg on the HAProxy server. In the backend section, add send-proxy-v2 and check-send-proxy to the Nginx server line:

frontend https_front
    bind *:443
    mode tcp
    option tcplog
    default_backend nginx_back

backend nginx_back
    mode tcp
    balance roundrobin
    server nginx1 10.0.0.20:443 check check-send-proxy send-proxy-v2

Technical note: The check-send-proxy parameter ensures HAProxy’s health check packets also include the Proxy Protocol header. Without it, Nginx will spam broken header "" while reading PROXY protocol into error.log every 2 seconds. If you have an older backend that doesn’t support v2, change send-proxy-v2 to send-proxy (v1). Once done, validate the syntax and reload HAProxy:

sudo haproxy -c -f /etc/haproxy/haproxy.cfg
sudo systemctl reload haproxy

Step 2: Configure Nginx to Accept Proxy Protocol

On the Nginx server (10.0.0.20), make sure the ngx_http_realip_module module is compiled in (most default packages on Ubuntu, Debian, and RHEL include it). Next, enable proxy_protocol on the listening port and declare HAProxy’s IP as trusted.

Open your virtual host config file (for example, /etc/nginx/sites-available/default) and update it as follows:

server {
    listen 443 ssl proxy_protocol;
    listen [::]:443 ssl proxy_protocol;

    server_name example.com;

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

    # Only trust Proxy Protocol headers from the HAProxy IP
    set_real_ip_from 10.0.0.10;
    real_ip_header proxy_protocol;

    location / {
        proxy_pass http://127.0.0.1:8080;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_protocol_addr;
    }
}

Here is what the 3 key directives do:

  • listen 443 ssl proxy_protocol; tells Nginx that every connection arriving on port 443 starts with a Proxy Protocol header (it automatically handles both v1 and v2).
  • set_real_ip_from 10.0.0.10; is a mandatory security safeguard. If you skip this, an attacker who connects directly to Nginx’s port could forge a Proxy Protocol header to spoof their IP.
  • real_ip_header proxy_protocol; overwrites the $remote_addr variable with the client’s real IP. As a result, directives like limit_req_zone and standard access logs automatically pick up the true IP without any application code changes.

Test the Nginx configuration and reload the service:

sudo nginx -t
sudo systemctl reload nginx

Step 3: Verify the Result and Deploy with Zero Downtime

From your local machine, send a test request to the domain pointing to HAProxy:

curl -I https://example.com

Immediately check the access log on the Nginx server:

sudo tail -n 5 /var/log/nginx/access.log

A working setup will show your machine’s public IP in the first column instead of 10.0.0.10:

203.0.113.45 - - [04/Dec/2026:10:15:22 +0000] "HEAD / HTTP/2.0" 200 0 "-" "curl/8.5.0"

Tip to avoid taking down production: Don’t enable proxy_protocol directly on a live port 443 serving users without testing first. Open a temporary secondary port on Nginx (for example, listen 8443 ssl proxy_protocol;) and create a test backend on HAProxy pointing to it. Once you confirm the logs record the real IP, switch the main port 443 to proxy_protocol and reload both services simultaneously.

Share: