How to Restore Visitor IPs Using mod_remoteip

August 30, 2024 / How-to Guide

When traffic passes through a proxy source like Cloudflare or Nginx, Apache may log the proxy’s IP address as the client’s IP instead of the original visitors. By enabling Apache’s mod_remoteip module, you can restore and log the true visitors IP address, ensuring accurate tracking of your site traffic.

Follow the steps:

  1. Access the server’s command line as the ‘root’ user via SSH or “Terminal” in WHM.
  2. Install the ea-apache24-mod_remoteip package.
    1. CentOS 7 or CloudLinux 7  –
      yum install ea-apache24-mod_remoteip
    2. CloudLinux 8/9, AlmaLinux 8/9, or Rocky Linux 8/9 –
      dnf install ea-apache24-mod_remoteip
    3. Ubuntu –
      apt install ea-apache24-mod_remoteip
  3. Open the /etc/apache2/conf.modules.d/370_mod_remoteip.conf file using your preferred text editor.
  4. Input the subsequent line under the LoadModule line.
    RemoteIPHeader X-Forwarded-For
  5. Add the proxy IP addresses as entries in the configuration file.
    1. Use ‘RemoteIPTrustedProxy’ for specifying remote IP addresses.
      RemoteIPTrustedProxy 203.0.113.2
    2. Use ‘RemoteIPInternalProxy’ for specifying local IP addresses.
      RemoteIPInternalProxy 192.0.2.2

      Please note that IP address ranges can be specified using CIDR notation.

  6. Save your changes and close the text editor.
  7. Log in to WHM as the ‘root’ user.
  8. Navigate to Home > Service Configuration > Apache Configuration > Global Configuration.
  9. Scroll down to the LogFormat (combined) option and select the textbox.
  10. Replace the default text with the following:
    %a %l %u %t \”%r\” %>s %b \”%{Referer}i\” \”%{User-Agent}i\”
  11. Next, select the textbox for the LogFormat (common) option.
  12. Replace the default text with the following:
    %a %l %u %t \”%r\” %>s %b
  13. Scroll to the bottom of the page and click the Save button.
  14. Finally, click the Rebuild Configuration and Restart Apache button.

In such a manner, you can restore visitor IPs using mod_remoteip. If you liked our article, you will surely love our support.