Multi country cache problem

Hugo Cisneiros (Eitch) hugo.cisneiros at gmail.com
Wed Oct 19 19:15:43 CEST 2011


On Wed, Oct 19, 2011 at 1:29 PM, Maikel Brouwer - Songteksten.nl
<mbrouwer at songteksten.nl> wrote:
> I've got the following problem. Maybe someone has a solution for it. My
> site is visited by two countries (Belgium and Netherlands). I have two
> different advertisement agencies to display ads for the specific
> countries. This means that every page how something like this:
>
> if ($country == 'be') {
>  //display stuff
> } else {
>  //display other stuff)
> }
>
> The problem is that when someone from 'be' visits the page when it's not
> yet cached the code in the 'be' block will be cached. When someone from
> 'nl' visits the page it will see the 'be' block.

You can modify the vcl_hash to cache content based on client's ip.

For example, you can create an acl with the IP ranges from two countries:

acl country1 {
   "192.168.0.0"/24;
}

acl country2 {
   "192.168.1.0"/24;
}

Then on vcl_hash:

sub vcl_hash {
   hash_data(req.url);

   if (req.http.host) {
        hash_data(req.http.host);
    } else {
        hash_data(server.ip);
    }

   if (client.ip ~ country1) {
      hash_data("country1");
   } elseif (client.ip ~ country2) {
      hash_data("country2");
   }
}

This would create TWO cache entries (it will duplicate your cache
size), one for country1 and one for country2. Actually, if the user
isn't from country1 and country2, it'll create another cache entry for
the rest (else). You can change this logic as you wish. The hash_data
is the key to create multiple cache entries depending on any usual
variable on the vcl.

--
[]'s
Hugo
www.devin.com.br




More information about the varnish-misc mailing list