Show X-Forwarded-For IP in Apache

When apache is running behind the proxy server it shows the IP of the proxy server as visitor IP. To fix this, you need to enable Apache module remoteip.

https://httpd.apache.org/docs/2.4/mod/mod_remoteip.html

On Ubuntu/Debian, this can be enabled with the command

Now create file

Add

IP_OF_YOUR_PROXY_SERVER_HERE = repace with your proxy server. This can be any proxy server like haproxy, nginx, etc.. If you have more than one proxy server, use IPs separated by space.

If your proxy IP is internal, use RemoteIPInternalProxy instead of RemoteIPTrustedProxy. On a server running varnish, RMOTE_ADDR shows 127.0.0.1 (varnish IP). To fix this, I used following

CF-Connecting-IP is because the site was behind cloudflare. Use X-Forwarded-For instead of CF-Connecting-IP if not using cloudflare.

Enable config with

To get Apache Logs to show real Visitor IP, replace %h with %a in LogFormat.

On Ubuntu

Find

Replace with

Restart Apache with

Now apache/php will show proper visitor IP instead of proxy server IP.

Need help with Linux Server or WordPress? We can help!

Leave a Reply

Your email address will not be published. Required fields are marked *