Many Drupal sites have the requirement to deliver different content based on a user's geographic location. There are many PHP solutions that will determine a user's location from their IP address so that a developer can write code to perform specific actions based on the result. However, when hosting behind a reverse proxy such as Varnish on Acquia Cloud, special considerations must be taken into account.
Acquia Cloud Enterprise and Acquia Cloud Site Factory customers with dedicated load balancers can request that GeoIP be enabled on their websites.
Once GeoIP is enabled for your website, you will need to test it. You can do this by checking the headers that Varnish adds when communicating with Drupal. The header information isn't visible to the browser or cURL, because the header is sent to the web server as a request header, but is not necessarily shown in the response headers.
The value of the lookup is available to Drupal via PHP using the server variable $_SERVER['HTTP_X_GEO_COUNTRY']
, or within .htaccess
as %{HTTP:X-Geo-Country}
. The two primary actions that a site might perform with this information are to either redirect the user to a specific URL or to serve country-specific content from the same URL.
If you expect to redirect the traffic to a new domain or path, you can take advantage of the X-Geo-Country
header in the .htaccess
file. One example would be:
RewriteCond %{HTTP:X-Geo-Country} ^IE$ [NC]
RewriteRule . http://ireland.mysite.com [L,R=301]
This detects IP addresses originating in Ireland, and redirects the user to the ireland
subdomain of the website. You can modify this based on countries from which you expect traffic, and direct them to the appropriate domains or pages.
If you are making actual content differences in Drupal based on X-Geo-Country
and serving them from the same URL, then you need to disable Drupal's internal page caching and rely on Varnish. Drupal does not have the ability to cache the content of the same URL separately for different values of the X-Geo-Country header
. For more information on that see https://www.drupal.org/project/drupal/issues/3023104 and related issue pages.To disable the Drupal page cache, you can:
Turn off the page_cache
module from the /admin/modules
page.
Add the following two lines to your settings.php
file:
$conf['cache_class_cache_form'] = 'DrupalDatabaseCache';
$conf['cache_class_cache_page'] = 'DrupalFakeCache';
This turns off the internal storage of the Drupal page cache, but retains the necessary headers and information for Varnish to cache the pages. Varnish will cache a separate copy of each page based on the combination of URL and X-Geo-Country
.
With the above done you should now also instruct Varnish to vary cache on the X-Geo-Country
header see Using custom Vary headers to create variations in Varnish cache.
Debugging headers
To see the headers that are being sent to Drupal, it is often handy to have a separate file that simply outputs the contents of the $_SERVER
variable.
headers.php
and place it in the docroot
.<?php
echo "<pre>";
print_r($_SERVER);
echo "</pre>";
?>
HTTP_X_GEO_COUNTRY
.Varnish calculates the X-Geo-Country
value based upon the IP address found in the X-Forwarded-For
header. To test, you need to override that header and put in an IP address from another country. A list of IP addresses, broken down by country, that you can use can be found at the NirSoft website.
Once you can view the the X-Geo-Country
header (see above Debugging headers section), you can test it by masquerading as a user from various locations. You can try one of the various browser spoofing tools available.
If this content did not answer your questions, try searching or contacting our support team for further assistance.
Wed Oct 22 2025 08:59:29 GMT+0000 (Coordinated Universal Time)