Cloudflare Docs
Support
Support
Visit Support on GitHub
Set theme to dark (⇧+D)

Configuring IP geolocation

​​ Overview

Cloudflare can include the country code of the visitor’s IP address (in  ISO 3166-1 Alpha 2 format) with each request between Cloudflare and the upstream origin web server. This allows site administrators to capture their visitor’s IP location in server logging and/or application logic. Besides ISO country codes, Cloudflare uses the following special country codes:

  • XX - Used for clients without country code data.
  • T1 - Used for clients using the Tor network.

The country code value is passed along in the CF-IPCountry request header to the origin web server. This header is not visible to visitors in the site’s HTTP response.

Cloudflare includes country code information for both IPv4 and IPv6 addresses. Currently, the IPv4 information is more robust, but we expect the IPv6 data to improve rapidly.


​​ Add IP geolocation information

The recommended procedure to enable IP geolocation information is to enable the Add visitor location headers Managed Transform. This Managed Transform adds HTTP request headers with location information for the visitor’s IP address, such as city, country, continent, longitude, and latitude.

Besides using the Managed Transform, you can also enable the IP Geolocation feature in the Cloudflare dashboard (Network app), which will only add a request header for the visitor’s country.


​​ Capturing IP geolocation data in server logs

Visitor traffic geolocation information can be captured in origin server logging. Below are two very common web server implementations and how a site administrator could configure custom logging for the country of their visitors. If you are using a Managed Transform to add geolocation information to requests, you can follow the same strategy for logging other geolocation values, like city or continent.

​​ Apache


LogFormat %{cf-ipcountry}i cloudflare_custom

More about  Apache LogFormat

​​ NGINX


log_format cloudflare_custom '"$http_cf_ipcountry"';

More about  NGINX log_format


​​ Capturing IP geolocation data in application logic

Web applications can also capture and use IP geolocation information in their logic. This is useful to direct visitors based on their country or provide defaults such as language and currency. Below are common language examples of how to capture this header into a variable for later use.

​​ PHP


$country_code = $_SERVER["HTTP_CF_IPCOUNTRY"];

​​ Python Flask


country_code = request.headers.get('cf-ipcountry')

Note that you’ll need to import python flask request module

​​ NodeJS


const country_code = headers['cf-ipcountry'];

Note that you’ll need to require http/https and instantiate the createServer() method

​​ C# (.NET)


string country_code = HttpContext.Current.Request.Headers.Get("cf-ipcountry");

​​ Perl


$country_code = $ENV{"HTTP_CF_IPCOUNTRY"};

​​ Troubleshooting

How to report a wrong IP location?

Cloudflare uses Maxmind as a geolocation database. Cloudflare updates its GeoIP database weekly.

You can use the Maxmind tool and make sure that Maxmind shows the correct Country Code. If the data is incorrect: