Using Apache’s mod_remoteip to log visitor true IP addresses
When your server is behind a reverse proxy, load balancer, or CDN, Apache’s access logs will typically show the IP address of the proxy server instead of the actual visitor’s IP address. Apache’s mod_remoteip module helps you see the original client IP address, which is crucial for accurate logging, statistics, and any IP-based access controls or applications.
This module overrides the client IP address for a request with the IP address reported by a trusted intermediate proxy or load balancer in a specific HTTP header, usually X-Forwarded-For.
Confirming if mod_remoteip is installed on a cPanel server
You can easily check if mod_remoteip is already enabled on your cPanel server via the command line.
First, access your server via SSH.
Next, run the following command to list all loaded Apache modules and search for remoteip_module:
apachectl -M | grep remoteip_moduleIf mod_remoteip is installed and active, you will see output similar to:
remoteip_module (shared)If the command returns no output, the module is not currently enabled.
Installing mod_remoteip via EasyApache 4
If mod_remoteip is not installed, you can easily add it using EasyApache 4 in your WebHost Manager (WHM) interface.
- Log into WHM as the ‘root’ user.
- In the search bar on the left, type “EasyApache” and click on EasyApache 4.
- Under “Currently Installed Packages,” click the Customize button.
- Click on the Apache Modules stage in the left-hand navigation.
- In the “Search Modules” field, type “remoteip”.
- You will see
mod_remoteipin the list. Toggle the switch to the right of it to enable it. The system will automatically select it for provisioning. - Click the Review stage in the left-hand navigation.
- Verify that
mod_remoteipis listed under “Packages to be installed.” - Scroll down and click the Provision button.
- Allow the provisioning process to complete. This may take a few minutes. Once done, you’ll see a “Done!” message.
After provisioning, Apache will automatically restart, loading the new module. You can re-run the command:
apachectl -M | grep remoteip_moduleto confirm it’s now active.
Configuring mod_remoteip
Once mod_remoteip is installed, you need to configure it by telling Apache which proxy servers to trust and which HTTP header contains the actual visitor’s IP address. This configuration is typically added to Apache’s main configuration file or, more commonly, within a specific include file for user customizations in cPanel environments.
For cPanel servers, the recommended way to add this configuration is through include files to ensure your changes are not overwritten by cPanel updates. You can create a .conf file in the directory /etc/apache2/conf.d/includes/. For example, you could create /etc/apache2/conf.d/includes/remoteip.conf.
Here’s an example configuration:
<IfModule remoteip_module>
RemoteIPHeader X-Forwarded-For
RemoteIPTrustedProxy 192.168.1.100
RemoteIPTrustedProxy 10.0.0.0/24
# Add additional RemoteIPTrustedProxy directives for each of your trusted proxy IPs or IP ranges.
</IfModule>Let’s break down these directives:
- RemoteIPHeader X-Forwarded-For: This line tells
mod_remoteipto look for the original client’s IP address in theX-Forwarded-ForHTTP header. This is the most common header used by proxies for this purpose. Other common headers includeX-Real-IP. Check your proxy or CDN provider’s documentation to confirm the correct header. - RemoteIPTrustedProxy 192.168.1.100: This line specifies an individual IP address of a trusted proxy server. Replace
192.168.1.100with the actual IP address of your proxy. - RemoteIPTrustedProxy 10.0.0.0/24: This line specifies a range of IP addresses for trusted proxy servers using CIDR notation. Replace
10.0.0.0/24with the actual IP range of your proxies.
Important: Only add the IP addresses of servers that you trust to provide accurate information in the RemoteIPHeader. If you trust a malicious proxy, it could spoof IP addresses.
You can add multiple RemoteIPTrustedProxy lines if you have more than one proxy server or IP range.
After creating or modifying your remoteip.conf file (or wherever you choose to place the configuration), you need to test the Apache configuration and then gracefully restart Apache for the changes to take effect.
Test your Apache configuration:
apachectl configtestIf you see “Syntax OK”, you can proceed to restart Apache.
Gracefully restart Apache:
systemctl restart httpdOnce Apache restarts, it will begin using mod_remoteip with your specified configuration, and your access logs should now reflect the true visitor IP addresses.
Verifying the configuration
After configuring mod_remoteip and restarting Apache, you should verify that it’s working correctly:
- Access your website through the proxy/CDN.
- Check your website’s Apache access logs (usually within
/var/log/apache2/domlogs/). - The IP address logged for your connection should now be your actual public IP address, not the IP address of the proxy server.
If you still see the proxy IP, double-check your mod_remoteip configuration, ensure the correct header is specified, and confirm that the proxy IPs are correctly listed as trusted.
By correctly configuring mod_remoteip, you ensure more accurate logging and allow any IP-dependent applications or security rules to function as intended.