I have a Magento site with two store views where the store is determined by geo IP (as long as no store cookie is explicitly set). This is done by setting the environment variable MAGE_RUN_CODE in index.php before the Magento application gets initialized:

/* Store redirect based on geo IP, only if store cookie is not set */
if (!isset($_COOKIE['store'])) {
    $cookieExpire = time() + 60 * 60 * 24 * 3;

    $storeCodes = array();
    foreach (Mage::app()->getStores() as $_store) {
        $storeCodes[] = $_store->getCode();

    $geoip = new Sandfox_GeoIP_Model_Country;
    $geoipCountryCode = $geoip->getCountry();
    $geoipCountryCode = strtolower($geoipCountryCode);

    if (in_array($geoipCountryCode, $storeCodes)) {
        setcookie('store', $geoipCountryCode, $cookieExpire, '/');
        $_SERVER['MAGE_RUN_CODE'] = $geoipCountryCode;

This works well until I enable Varnish (using the Turpentine extension).

  • users will receive the page from cache on first visit
  • the content of the cache is determined by the IP of the first visitor for each page

I can imagine a solution with a custom VCL, but unfortunately I cannot change it and have to use the default one from Turpentine.

So what I need is a way to bypass Varnish for every new visitor, determine their store view based on the IP and then serve the cached page for this store view.

Any ideas?

  • what kind of Geo IP detection and redirect method do you use?
    – Pawel
    Commented Oct 6, 2016 at 11:13
  • @Pawel Until now, no redirection, I'm just setting the store code in index.php. IP detection comes from github.com/magento-hackathon/Sandfox_GeoIP Commented Oct 6, 2016 at 11:16
  • I added the source code, in case it's relevant. Commented Oct 6, 2016 at 11:19
  • In my opinion it's very hard without changing the VCL. I don't know turpentine very well, but there is a placeholder where you can set your custom VCL without modifying the default VCL. Is there a way in you project to set this custom VCL?
    – Pawel
    Commented Oct 6, 2016 at 11:26
  • Doesn't varnish use sessions instead of cookies? Commented Oct 6, 2016 at 12:20

1 Answer 1


you have probably only few options:

  1. use geoip mod and custom varnish config to pass proper geoip hash to backend.
  2. use "VCL Fix = on" in Turpentine settings, it will bypass request if no cookies or magento session. (caution: high load)
  3. set all geoip cookies and variables with javascript on client side.

better to use option 1.

but in different projects we use option 1+3

ps. varnish 3 version this way to add hash for geoip, per country:

import geoip;
     sub vcl_recv {
         set req.http.X-Forwarded-For = client.ip;
         set req.http.X-Client-GeoIP = geoip.country_code(req.http.X-Forwarded-For);

sub vcl_hash {

actually just found this blog post which explains it better: http://blog.tenya.me/blog/2013/10/25/geoip-with-varnish/

  • Thanks for the suggestions, it was helpful! I ended up using an AJAX request that sets the store cookie based on location, if no store cookie has been set yet (so basically option 3). It works well enough for my case within my limitations. Commented Oct 18, 2016 at 13:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.