Restoring original visitor IPs

Last updated Apr 23, 2026 When your website traffic is routed through the Cloudflare network, we act as a reverse proxy. This allows Cloudflare to speed up page load time by routing packets more efficiently and caching static resources (images, JavaScript, CSS, etc.). As a result, when responding to requests and logging them, your origin server returns a Cloudflare IP address ↗︎.

For example, if you install applications that depend on the incoming IP address of the original visitor, a Cloudflare IP address is logged by default. The original visitor IP address appears in an appended HTTP header called CF-Connecting-IP. By following our web server instructions, you can log the original visitor IP address at your origin server. If this HTTP header is not available when requests reach your origin server, check your Transform Rules and Managed Transforms configuration.

Note

If Pseudo IPv4 is set to Overwrite Headers - Cloudflare overwrites the existing Cf-Connecting-IP and X-Forwarded-For headers with a pseudo IPv4 address while preserving the real IPv6 address in CF-Connecting-IPv6 header.

The diagram below illustrates the different ways that IP addresses are handled with and without Cloudflare.

The diagram illustrates the different ways that IP addresses are handled with and without Cloudflare.

Caution

Cloudflare no longer updates and supports mod_cloudflare, starting with versions Debian 9 and Ubuntu 18.04 LTS of the Linux operating system. We now recommend mod_remoteip ↗︎ for customers using Apache web servers. Customers who are interested in building the mod_cloudflare package can download the codebase ↗︎ from GitHub.


mod_remoteip

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.

As this module was created by an outside party, we can't provide technical support for issues related to the plugin.

To install mod_remoteip on your Apache web server:

  1. Enable mod_remoteip by issuing the following command:
sudo a2enmod remoteip
  1. Update the site configuration to include RemoteIPHeader CF-Connecting-IP, e.g.  /etc/apache2/sites-available/000-default.conf
ServerAdmin webmaster@localhost
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
  1. 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
  1. 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 192.0.2.1 (example IP address)
RemoteIPTrustedProxy 192.0.2.2 (example IP address)
(repeat for all Cloudflare IPs listed at https://www.cloudflare.com/ips/)
  1. Enable Apache configuration:
sudo a2enconf remoteip
Enabling conf remoteip.
To activate the new configuration, you need to run:
service apache2 reload
  1. Test Apache configuration:
sudo apache2ctl configtest
Syntax OK
  1. Restart Apache:
sudo systemctl restart apache2

Note

For more information on mod_remoteip, refer to the Apache documentation ↗︎.


mod_cloudflare

Caution

Cloudflare no longer updates and supports mod_cloudflare, starting with versions Debian 9 and Ubuntu 18.04 LTS of the Linux operating system. We now recommend mod_remoteip ↗︎ for customers using Apache web servers. Customers who are interested in building the mod_cloudflare package can download the codebase ↗︎ from GitHub.

Installing

There are two methods for installing mod_cloudflare: by downloading the Apache extension from GitHub or by adding code to your origin web server.

Downloading packets or scripts from GitHub

If you are using an Apache web server, you can download mod_cloudflare from GitHub ↗︎.

Adding code to your origin web server

If you can't install mod_cloudflare, or if there is no Cloudflare plugin available for your content management system platform to restore original visitor IP, add this code to your origin web server in or before the <body> tag on any page that needs the original visitor IPs:

<?php if (isset($_SERVER['HTTP_CF_CONNECTING_IP'])) $_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_CF_CONNECTING_IP'];?>

This command will only make the IP address available to scripts that need it. It doesn’t store the IP in your actual server logs.

Removing

Apache

To remove mod_cloudflare, you should comment out the Apache config line that loads mod_cloudflare.

This varies based on your Linux distribution, but for most people, if you look in /etc/apache2, you should be able to search to find the line:

LoadModule cloudflare_module

Comment or remove this line, then restart apache, and mod_cloudflare should be gone.

If you are running Ubuntu or Debian, you should see.

file/etc/apache2/mods-enabled/cloudflare.load

delete this file to remove mod_cloudflare, then restart Apache.

Nginx

mod_cloudflare is not needed for Nginx. Use the ngx_http_realip_module NGINX module ↗︎ and the configuration parameters described in the Web server instructions ↗︎ instead.


Web server instructions

Refer below for instructions on how to configure your web server to log original visitor IPs based on your web server type:

Apache 2.4

Caution

Cloudflare no longer updates and supports mod_cloudflare, starting with versions Debian 9 and Ubuntu 18.04 LTS of the Linux operating system. We now recommend mod_remoteip ↗︎ for customers using Apache web servers. Customers who are interested in building the mod_cloudflare package can download the codebase ↗︎ from GitHub.

  1. Make sure the following is installed:
  2. Red Hat/Fedora sudo yum install httpd-devel libtool git
  3. Debian/Ubuntu sudo apt-get install apache2-dev libtool git
  4. Clone the following for the most recent build of mod_cloudflare:
  5. Red Hat/Fedora/Debian/Ubuntu: git clone https://github.com/cloudflare/mod_cloudflare.git; cd mod_cloudflare
  6. Use the Apache extension tool to convert the .c file into a module:
  7. Red Hat/Fedora/Debian/Ubuntu: apxs -a -i -c mod_cloudflare.c
  8. Restart and verify the module is active:
  9. Red Hat/Fedora service httpd restart; httpd -M|grep cloudflare
  10. Debian/Ubuntu: sudo apachectl restart; apache2ctl -M|grep cloudflare
  11. If your web server is behind a load balancer, add the following line to your Apache configuration (httpd.conf usually) and replace 123.123.123.123 with your load balancer's IP address:
IfModule cloudflare_module
CloudFlareRemoteIPHeader X-Forwarded-For
CloudFlareRemoteIPTrustedProxy [insert your load balancer’s IP address]
DenyAllButCloudFlare
/IfModule

Nginx

Use the ngx_http_realip_module Nginx module ↗︎ and the following configuration parameters:

#example IP address
set_real_ip_from 192.0.2.1;

#use any of the following two

real_ip_header CF-Connecting-IP;
#real_ip_header X-Forwarded-For;

That list of prefixes needs to be updated regularly, and we publish the full list in  Cloudflare IP addresses ↗︎.

Note

To Include the original visitor IP in your logs, add the variables $http_cf_connecting_ip and $http_x_forwarded_for in the log_format directive.

Also refer to:  Cloudflare and NGINX ↗︎.

EasyApache and cPanel

Caution

Cloudflare no longer updates and supports mod_cloudflare, starting with versions Debian 9 and Ubuntu 18.04 LTS of the Linux operating system. We now recommend mod_remoteip ↗︎ for customers using Apache web servers. Customers who are interested in building the mod_cloudflare package can download the codebase ↗︎ from GitHub.

  1. Run the following script to install mod_cloudflare as part of EasyApache: bash <(curl -s https://raw.githubusercontent.com/cloudflare/mod_cloudflare/master/EasyApache/installer.sh)
  2. Upon installing, you will need to recompile your Apache with the new mod_cloudflare plugin.
  3. To fix this, open up your Apache configuration. This can typically be found in  /etc/apache2/apache2.conf,  /etc/httpd/httpd.conf,  /usr/local/apache/conf/httpd.conf or another location depending on configuration. If you're unsure, ask your hosting provider.
  4. At the very end add: CloudflareRemoteIPTrustedProxy {LOOPBACK_ADDRESS} So, if your server is located at 127.0.0.1, it will look like: CloudflareRemoteIPTrustedProxy 127.0.0.1
  5. If you have more than one server to add to the trusted proxy list, you can add them at the end: CloudflareRemoteIPTrustedProxy 127.0.0.1 127.0.0.2

Lighttpd

To have Lighttpd automatically rewrite the server IP for the access logs and for your application, you can follow one of the two solutions below.

  1. Open your lighttpd.conf file and add mod_extforward to the server.modules list. It must come after mod_accesslog to show the real IP in the access logs
  2. Add the following code block anywhere in the lighttpd.conf file after the server modules list and then restart Lighttpd
$HTTP["remoteip"] == "192.2.0.1 (example IP address)"
{
extforward.forwarder = ( "all" => "trust" )
extforward.headers = ("CF-Connecting-IP")
}

Note

If your origin connects to the Internet with IPv6, $HTTP["remoteip"], which is required for matching the remote IP ranges does not work when IPv6 is enabled. Using the above method will not work when trying to forward IP ranges. Add the following lines to lighttpd.conf as an alternative solution: extforward.forwarder = ( "all" => "trust" ) extforward.headers = ("CF-Connecting-IP")

LiteSpeed server

  1. Go to your LiteSpeed Web Admin Console.
  2. Enable the option Use Client IP in Header in Configuration.
  3. Once enabled, your access logs will now show the correct IP addresses, and even PHP's $_SERVER['REMOTE_ADDR'] variable will contain the client real IP address, instead of a Cloudflare IP address, which in itself will resolve most problems you could hit when enabling Cloudflare on PHP-enabled web sites (like WordPress or vBulletin installs).

Microsoft IIS

For IIS 7 - 8:

Follow the directions in the Microsoft Community ↗︎.

For IIS 8.5 - 10:

From IIS 8.5 onwards, custom logging is a built-in option. Refer to IIS Enhanced Logging ↗︎.

  1. In IIS Manager, double click on Logging in the Actions menu of the site you are working on.
  2. After this launches, select W3C as the format and then click Select Fields next to the format drop-down in the Log File sub-section.
  3. Click on Add Field and add in CF-Connecting-IP header.
  4. Click Ok. You should see your new entry reflected under Custom Fields. Click on Apply when you are back in the Logging window.
  5. If this is successful, the log file should now have an underscore:You should also see the change in the fields:
  6. Restart the site, then W3SVC, then the entire instance if the change doesn’t reflect immediately.When using enhanced logging in IIS 8.5+, it does not restore original visitor IP at the application level.