Behind a reverse proxy, Apache can log the X-Forwarded-For address chain alongside the connecting proxy address for request tracing.
Apache access logging is handled by mod_log_config. A named LogFormat string decides which request fields are written, and Apache can attach more than one CustomLog directive to the same virtual host, so you can keep the standard combined log and add a second file for forwarded-client visibility. The %{c}a field keeps the underlying TCP peer address even when a module changes the client address used for the request.
The X-Forwarded-For value can contain a comma-separated chain, and clients can supply forged values. Recording the header does not establish a trusted client identity, which depends on a controlled proxy path and an explicit trust policy. A dedicated forwarded-header log also avoids changing the format expected by existing access-log parsers.
If mod_remoteip consumes X-Forwarded-For through RemoteIPHeader, it can shorten or remove the header before access logging. This format records the remaining header, not a preserved original. This method supports the complete incoming chain only when no module rewrites the header; with mod_remoteip consuming it, original-header retention belongs in a log at the upstream proxy. Disabling trusted-client handling just to populate this log can change IP-based access decisions.
The extra file duplicates request records and stores forwarded addresses. Its storage, retention, and access permissions need the same care as the existing access log.
Steps to log X-Forwarded-For headers in Apache:
- Identify the virtual host that serves the site.
$ sudo apache2ctl -S
The output identifies the configuration file and line for each active virtual host. The file paths below use the Debian and Ubuntu layout; other installations may use /etc/httpd/.
- Locate the existing LogFormat and CustomLog directives.
$ sudo grep -R -n 'LogFormat\|CustomLog' /etc/apache2
The search also lists inactive files in sites-available, so the virtual-host path from apache2ctl -S identifies the site to change. On RHEL-style systems, the configuration directory is usually /etc/httpd/.
- Open the file that defines the current combined log format.
$ sudoedit /etc/apache2/apache2.conf
Some platforms define the default format in /etc/httpd/conf/httpd.conf or a distro-specific include file. Edit the file shown by the previous grep output instead of guessing.
- Insert the new combined_xff format immediately after the existing combined format.
LogFormat "\"%{X-Forwarded-For}i\" %{c}a %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined_xffThe format definition belongs before the include that loads the virtual hosts using it. The quoted first field is the header available at logging time; %{c}a is the connected peer, while %a can be changed by mod_remoteip.
If your platform's current combined format uses %b instead of %O, keep the existing byte field and add only the quoted X-Forwarded-For value plus %{c}a.
- Save the Apache configuration file.
- Open the active virtual-host configuration file.
$ sudoedit /etc/apache2/sites-available/000-default.conf
Use the file path from the initial grep output when the active CustomLog directive lives in a different virtual host or include file.
- Insert a second CustomLog directive directly beneath the existing access-log directive.
CustomLog ${APACHE_LOG_DIR}/access.log combined CustomLog /var/log/apache2/access_xff.log combined_xffThe existing CustomLog line stays unchanged; only the new line is added. The new destination uses the same log directory, but rotation and retention still depend on the rules covering that filename.
- Save the virtual-host configuration file.
- Test the Apache configuration syntax before reloading it.
$ sudo apache2ctl -t Syntax OK
Use sudo apachectl -t or sudo httpd -t on platforms that do not ship the apache2ctl wrapper.
- Reload Apache so the new log format and destination become active.
$ sudo systemctl reload apache2
Use sudo systemctl reload httpd on most RHEL-family systems, or sudo apachectl graceful when systemd is not managing the service.
- Send a test request with an X-Forwarded-For header.
$ curl --silent --output /dev/null --write-out "%{http_code}\n" --header "X-Forwarded-For: 203.0.113.10, 198.51.100.7" http://127.0.0.1/ 200Use the site hostname or add a matching Host header when localhost does not map to the virtual host you changed.
- Confirm that the new log records the header chain and the connection peer.
$ sudo cat /var/log/apache2/access_xff.log "203.0.113.10, 198.51.100.7" 127.0.0.1 - - [05/Nov/2026:07:47:58 +0000] "GET / HTTP/1.1" 200 10927 "-" "curl/8.18.0"
The quoted first field contains the two addresses supplied by the test. The next field is 127.0.0.1 because curl connected directly to Apache; it is the connecting proxy address for proxied requests. This proves header recording, not that either supplied address is the real client.
A missing header appears as "-". A shortened chain or a dash can also result from mod_remoteip consuming the header. Older log rows can be distinguished by the request timestamp and header value.
On Debian and Ubuntu, the default /etc/logrotate.d/apache2/ wildcard usually covers /var/log/apache2/*.log. On RHEL-style systems, confirm the new filename is included in the httpd logrotate rules.
Tool: Proxy Server Checker