Skip to main content

How to Preserve the Original Visitor IP Address Behind Cloudflare

You can configure your server to identify the original visitor's IP address instead of the Cloudflare proxy IP. This is necessary for customers using Cloudflare to ensure that security logs, geo-location features, and access controls function correctly.

You can configure your server to identify the original visitor's IP address instead of the Cloudflare proxy IP. This is necessary for customers using Cloudflare to ensure that security logs, geo-location features, and access controls function correctly.

Important: This feature is server-dependent. The specific method for identifying the original IP depends on your server's operating system, web server software (such as Apache or Nginx), and the specific applications you are running.

Before you begin

You will need:

  • A website currently using Cloudflare as a proxy.

  • Administrative access to your server or web server configuration files.

  • Knowledge of your specific web server software (e.g., Apache, Nginx, or IIS).

Configure your server to recognize the X-Forwarded-For header

Cloudflare passes the original visitor's IP address in the X-Forwarded-For HTTP header. To ensure your applications and logs record this value instead of the Cloudflare proxy IP, you must configure your web server to trust the Cloudflare proxy.

For Apache Web Servers

If you are using Apache, you must enable mod_remoteip and configure it to trust the Cloudflare IP ranges.

  1. Open your Apache configuration file (e.g., httpd.conf or apache2.conf).

  2. Locate or create the RemoteIP_Header directive.

  3. Set the configuration to use the X-Forwarded-For header:

    RemoteIP_Header X-Forwarded-For

  4. Define the trusted proxies (Cloudflare IP ranges) to ensure the server only accepts the header from Cloudflare.

  5. Restart the Apache service.

For Nginx Web Servers

If you are using Nginx, you must use the real_ip module.

  1. Open your Nginx configuration file (e.g., nginx.conf or your specific site configuration).

  2. Add the following directives to your configuration:

    set_real_ip_from 103.0.0.0/8; # Example: Replace with actual Cloudflare IP ranges
    real_ip_header X-Forwarded-For;

  3. Replace the example range with the current list of Cloudflare IP addresses.

  4. Restart the Nginx service.

For Windows Server (IIS)

If you are using IIS, you may need to use a module such as "IIS Rewrite" or a specific "Proxy" module to extract the header.

  1. Open the IIS Manager.

  2. Select your website and open the URL Rewrite module.

  3. Create a rule or use a module like X-Forwarded-For to pass the header to the application.

  4. Ensure the application is configured to read the X-Forwarded-For header for its logging and logic.

Confirm the result

To verify that your server is correctly identifying the visitor's IP:

  • Check your server's access logs. The IP address listed should be the visitor's actual IP, not a Cloudflare IP address.

  • If your application has a "Whois" or "IP Lookup" feature, verify that it displays the correct location for your current connection.

Limitations

  • Manual Updates: Cloudflare occasionally updates its IP ranges. If you use a manual list in your configuration, you may need to update it periodically to ensure continued accuracy.

  • Application Support: Some applications (such as WordPress plugins or custom scripts) may have their own internal settings to "trust" proxies. If the server-level configuration above does not resolve the issue, check the specific settings within your application.

  • Standard Headers: This procedure specifically addresses the X-Forwarded-For header. Other headers like CF-Connecting-IP are also provided by Cloudflare but may require different configurations depending on your software.

Did this answer your question?