How to Restore Visitor IP Addresses in Apache

When Apache sits behind a reverse proxy or load balancer, the TCP connection to Apache comes from that intermediary. The address Apache sees by default is therefore the proxy address, even though a separate request header may carry the original visitor address. Apache's mod_remoteip module can use that header to set an effective client address for logging and some access rules.
A client can send a forwarding header of its own. If Apache accepts that header from any connection, an untrusted client can make Apache log a forged IP address. Identify the proxies that are allowed to provide address information, restrict trust to them, and test a direct request with a forged header before relying on the result.
Identify the Actual Proxy Chain
Map the actual request path from visitor to Apache, including any CDN, load balancer, or other reverse proxy in that path. Record which component connects directly to Apache and which one sets or appends the chosen forwarding header. Confirm the address ranges that Apache will see for those immediate peers. A public CDN address list, a private load-balancer subnet, and a single local proxy need different trust entries.
Choose the forwarding header that your proxy actually sets. X-Forwarded-For is common, but some services use a different field. The first trusted proxy receiving traffic from an untrusted client must record the address it observed, rather than pass on an unchecked client-supplied value as the visitor address.
Later proxies may append their observed peer addresses to an established chain. Confirm that each proxy handles the header as expected, and keep the Apache trust list limited to those proxies. Update the list when the proxy provider changes its address ranges.
Configure mod_remoteip with Trusted Peers
Enable mod_remoteip using the supported method for your Apache installation. Add the header and proxy settings to the server configuration or the relevant virtual host. For a deployment with one proxy that sets X-Forwarded-For, the configuration has two parts:
RemoteIPHeader X-Forwarded-For
RemoteIPTrustedProxy 192.0.2.20The address 192.0.2.20 is a documentation example; replace it with the actual proxy address Apache sees. RemoteIPHeader selects the header, while RemoteIPTrustedProxy identifies the peers allowed to supply it. Do not configure the header alone: without proxy trust restrictions, mod_remoteip can accept reported addresses from arbitrary connections.
Choose the trust directive according to the addresses the proxy needs to report. RemoteIPTrustedProxy does not accept reported private IPv4 addresses, IPv4 loopback or link-local addresses, or IPv6 addresses outside 2000::/3. RemoteIPInternalProxy permits those reported addresses, so use it only for controlled proxies that need to report them. A proxy's own private IP address does not, by itself, require RemoteIPInternalProxy.
Check the edited configuration with apachectl -t or your installation's equivalent syntax check. Resolve any errors before applying the change with a graceful reload through the supported service-management method. Then check the logs to confirm that the running server accepted the configuration.
Apache processes comma-separated reported addresses from right to left, stopping when it reaches a hop that was not trusted to present the preceding address. This means the effective client depends on the entire trusted chain, not a blind selection of the first string in a header. If there are several proxies, document which ones are trusted and how each modifies the header.
Log Both Effective and Connection Addresses
After mod_remoteip changes the effective address for a request, Apache's %a log token reports that effective address. The %{c}a token reports the underlying connection peer. During rollout, record both so you can compare the reported client address with the expected proxy address.
LogFormat "%a %{c}a %t %>s" proxy_audit
CustomLog logs/proxy-audit.log proxy_auditThis example defines a format and enables a separate log using that format. Each entry records the effective address, connection address, timestamp, and final response status. The relative log path is under Apache's ServerRoot; use an appropriate protected log directory for your installation.
Do not mistake a plausible address in %a for proof of a person's identity. Shared networks, VPNs, carrier NAT, and compromised clients can all affect the apparent address. Because mod_remoteip also influences address-based authorization, a trust error can have more serious consequences than misleading analytics. Review any Require ip rules that will consume the effective value.
Test Proxied and Direct Requests
From an approved test client, send a request through the normal proxy path and compare the effective and connection addresses in the log. The connection peer should be one of the trusted proxies. The effective address should match the test client's address as observed by the first trusted proxy. Record the test environment and observed values without putting personal addresses in public documentation.
Then, in an authorized test environment, send a request directly to Apache with a deliberately false forwarding header. Use the same hostname and virtual host as the proxied test, from a client whose connection address is not in the trust list. A well-formed test value such as the documentation address 203.0.113.99 avoids confusing malformed-header rejection with correct trust enforcement.
The effective address must not switch to the forged value simply because the header is present. If direct access is forbidden by network policy, verify that the direct connection is blocked and separately test trust behavior in a controlled local setup. Do not probe infrastructure outside your administration scope.
If the test fails, inspect the actual connection peer, proxy header behavior, and trust directives before widening a range. An overly broad private subnet or unrestricted header acceptance can make the log look correct for normal traffic while leaving a spoofing path open.
Minimize Retained Address Data
Keeping both addresses during validation is useful, but decide how long detailed logs are needed and who may read them. Remove temporary diagnostic fields when the deployment is stable if they are not needed for operations. Choose a retention period based on your organization's operational and privacy requirements.
After adding a CDN, changing load balancers, or altering direct-origin access, repeat the address comparisons and the direct-request check. A previously correct trust list may no longer match the request path.







