You put HAProxy in front of two Apache servers, everything balances nicely, and then you open the access log on one of the web servers. Every single request comes from the same IP: the load balancer.

This guide explains why that happens, the quick fix (log the X-Forwarded-For header), and the cleaner fix with mod_remoteip, plus the detail most guides skip: the quick fix can be faked by any client.

The setup

  • balancer: HAProxy, public side 192.168.10.10, internal side 192.168.100.1.
  • apache1 (192.168.100.100) and apache2 (192.168.100.101): Apache with PHP on Debian.
  • The client is a laptop at 192.168.10.1.

HAProxy backend, round robin with health checks:

frontend web
    bind *:80
    default_backend web_servers

backend web_servers
    balance roundrobin
    option httpchk GET /
    server apache1 192.168.100.100:80 check
    server apache2 192.168.100.101:80 check

Requests alternate between the two servers, as expected:

$ for i in 1 2 3 4; do curl -s http://www.example.org/ | grep Server; done
<h1>Server: apache1</h1><p>Client IP: 192.168.100.1</p>
<h1>Server: apache2</h1><p>Client IP: 192.168.100.1</p>
<h1>Server: apache1</h1><p>Client IP: 192.168.100.1</p>
<h1>Server: apache2</h1><p>Client IP: 192.168.100.1</p>

Look at “Client IP”: it is 192.168.100.1, the balancer. The Apache log says the same:

debian@apache1:~$ sudo grep curl /var/log/apache2/access.log | tail -n 2
192.168.100.1 - - [29/Oct/2026:13:53:42 +0100] "GET / HTTP/1.1" 200 335 "-" "curl/8.14.1"
192.168.100.1 - - [29/Oct/2026:13:53:42 +0100] "GET / HTTP/1.1" 200 335 "-" "curl/8.14.1"

Why: there are two TCP connections, not one

A reverse proxy or load balancer does not “pass through” the client’s connection. There are two separate connections:

client 192.168.10.1  --TCP-->  HAProxy  --TCP (new connection)-->  apache1

Apache’s %h in the log format is the source IP of the TCP connection it received. That connection was opened by HAProxy, so Apache can only ever see HAProxy’s address there. Nothing is misconfigured; the client’s IP simply isn’t in that connection.

The only way to get it to Apache is to put it inside the HTTP request, as a header.

Step 1: Make HAProxy send X-Forwarded-For

Add option forwardfor to the backend (or to defaults):

backend web_servers
    option forwardfor
    balance roundrobin
    option httpchk GET /
    server apache1 192.168.100.100:80 check
    server apache2 192.168.100.101:80 check

Check and reload:

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

Now each request reaching Apache carries X-Forwarded-For: 192.168.10.1. But Apache does not log that header by default, so the log still shows the balancer. You have two ways to use it.

Step 2, quick way: log the header

Create a log format that writes the header instead of %h. On Debian, put it in its own config snippet:

$ sudo nano /etc/apache2/conf-available/balanced-log.conf
LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %O" balanced
$ sudo a2enconf balanced-log

Then use it in the site. In /etc/apache2/sites-available/000-default.conf, change the CustomLog line:

CustomLog ${APACHE_LOG_DIR}/access.log balanced
$ sudo apache2ctl configtest && sudo systemctl reload apache2

Do the same on every backend server. Now the log shows the real client:

debian@apache1:~$ sudo grep -v "HTTP/1.0" /var/log/apache2/access.log | tail -n 2
192.168.10.1 - - [29/Oct/2026:18:57:40 +0100] "GET / HTTP/1.1" 200 335
192.168.10.1 - - [29/Oct/2026:18:57:40 +0100] "GET / HTTP/1.1" 200 335

Why the grep -v "HTTP/1.0"?

Your log is now full of lines with an empty client field and HTTP/1.0. Those are HAProxy’s health checks (option httpchk): they come from HAProxy itself, with no X-Forwarded-For, every couple of seconds. Filtering out HTTP/1.0 is a quick way to read only real traffic.

The problem with the quick way: anyone can fake the header

X-Forwarded-For is just a request header. A client can send its own:

$ curl -H "X-Forwarded-For: 1.2.3.4" http://www.example.org/

By default, option forwardfor adds the real address to the header the client sent rather than replacing it, so Apache receives something like 1.2.3.4, 192.168.10.1. With the quick log format, that whole string goes into your log, and anything reading the first value will believe the request came from 1.2.3.4.

For a lab, that’s fine. For anything where logs matter (fail2ban, abuse reports, rate limiting in the app), use the next method.

Step 2, better way: mod_remoteip

mod_remoteip makes Apache replace the connection’s client IP with the one from the header, but only when the request comes from a proxy you trust. Everything that uses the client IP (logs, Require ip, PHP’s REMOTE_ADDR) then sees the real client.

Enable it:

$ sudo a2enmod remoteip
$ sudo nano /etc/apache2/conf-available/remoteip.conf
RemoteIPHeader X-Forwarded-For
RemoteIPInternalProxy 192.168.100.1
  • RemoteIPHeader: which header holds the client IP.
  • RemoteIPInternalProxy: the balancer’s address as Apache sees it. Use your HAProxy’s internal IP. Requests from any other address are not trusted, so a client hitting Apache directly cannot inject a fake IP.
$ sudo a2enconf remoteip

One more change, which is the part people miss: Debian’s default combined format logs %h, the raw connection address. mod_remoteip updates %a. Edit the LogFormat ... combined line in /etc/apache2/apache2.conf and change the leading %h to %a:

LogFormat "%a %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined

Keep the site’s CustomLog as combined, then:

$ sudo apache2ctl configtest && sudo systemctl reload apache2

Which one should you use?

Log %{X-Forwarded-For}imod_remoteip
SetupOne LogFormatModule + trusted proxy + %a
LogsReal IPReal IP
App sees real IP (REMOTE_ADDR)NoYes
Spoofable by the clientYesNo (only trusted proxy is believed)

Checklist

  1. HAProxy: option forwardfor in the backend, reload.
  2. Apache, quick: LogFormat "%{X-Forwarded-For}i ..." and use it in CustomLog.
  3. Apache, proper: a2enmod remoteip, RemoteIPHeader X-Forwarded-For, RemoteIPInternalProxy <balancer IP>, and %h → %a in the log format.
  4. Repeat on every backend server.
  5. Ignore the HTTP/1.0 lines: they are HAProxy health checks.