Learn how to install mod_remoteip and log your visitor’s original IP address at your Apache origin server.
Overview
Cloudflare no longer updates and supports mod_cloudflare. However, if you are using an Apache web server with an operating system such as Ubuntu Server 18.04 and Debian 9 Stretch, you can use mod_remoteip to log your visitor’s original IP address.
Installing mod_remoteip
To install mod_remoteip on your Apache web server:
1. Enable mod_remoteip by issuing the following command:
sudo a2enmod remoteip
2. Update the site configuration to include RemoteIPHeader CF-Connecting-IP, e.g. /etc/apache2/sites-available/000-default.conf
<VirtualHost *:80> ServerAdmin [email protected] DocumentRoot /var/www/html ServerName remoteip.andy.support RemoteIPHeader CF-Connecting-IP ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>
3. Update combined LogFormat entry in apache.conf
, replacing %h with %a in /etc/apache2/apache2.conf.
For example, if your current LogFormat appeared as follows:
LogFormat "%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined
you would update LogFormat to the following:
LogFormat "%a %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined
4. Define trusted proxy addresses by creating /etc/apache2/conf-available/remoteip.conf
by entering the following code and Cloudflare IPs:
RemoteIPHeader CF-Connecting-IP RemoteIPTrustedProxy 173.245.48.0/20 RemoteIPTrustedProxy 103.21.244.0/22 RemoteIPTrustedProxy 103.22.200.0/22 RemoteIPTrustedProxy 103.31.4.0/22 RemoteIPTrustedProxy 141.101.64.0/18 RemoteIPTrustedProxy 108.162.192.0/18 RemoteIPTrustedProxy 190.93.240.0/20 RemoteIPTrustedProxy 188.114.96.0/20 RemoteIPTrustedProxy 197.234.240.0/22 RemoteIPTrustedProxy 198.41.128.0/17 RemoteIPTrustedProxy 162.158.0.0/15 RemoteIPTrustedProxy 104.16.0.0/12 RemoteIPTrustedProxy 172.64.0.0/13 RemoteIPTrustedProxy 131.0.72.0/22 RemoteIPTrustedProxy 2400:cb00::/32 RemoteIPTrustedProxy 2606:4700::/32 RemoteIPTrustedProxy 2803:f800::/32 RemoteIPTrustedProxy 2405:b500::/32 RemoteIPTrustedProxy 2405:8100::/32 RemoteIPTrustedProxy 2a06:98c0::/29 RemoteIPTrustedProxy 2c0f:f248::/32
5. Test Apache configuration:
sudo apache2ctl configtest Syntax OK
6. Restart Apache:
sudo systemctl restart apache2
FAQs
- Apache won't pass CF-Connecting-IP down the stack. For example, phpinfo will not return this header at all whilst our edge will send it to Apache:
<? phpinfo(); ?>