Question

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?

Was it helpful?

Solution

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 {
...
hash_data(req.http.X-Client-GeoIP);
...
}

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

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top