Varnish to redirecting users based on their IP

Events happening in the community are now at Drupal community events on www.drupal.org.
pris54's picture

Apologies in advance if this is not the best place to ask for help, I've been looking for the most relevant group and this one seems the most appropriate.

I'm trying to get Varnish to redirect users based on their IPs, for example:

1) Users with IPs from Cambodia, Indonesia, Malaysia and Thailand should be redirected to [my website]/my (this is the Malaysian version)

2) Users with IPs from Bangladesh China, Hong Kong, Japan and Singapore should be redirected to [my website]/sg

3) The rest of the IPs remain in my default site

On top of this, if the user overrides this rule by picking a different version of the site (for example: I'm in Thailand but I deliberately choose to see the UK version) I need Varnish to remember this, maybe using a cookie?

Would having these rules in place have an impact in the site's performance?

I've been googling for a snippet or tutorial or any help but I couldn't find anything so far. Could I please get some advice?

Many thanks in advance,
P.

Comments

you could use

btopro's picture

you could use https://drupal.org/project/geoip as the library for detecting what regions are incoming.
https://drupal.org/project/geoip_redirect is D6 but could glance at as example code as to how you could issue the bounce in drupal potentially.

I agree with the geoip

beanjammin's picture

I agree with the geoip recommendation, I am using it exactly as the OP wants on a site behind varnish. We are using the multilingual support for the redirection rather than geoip_redirect.

You do not want to use varnish to do the redirection directly as it will force an expensive (performance wise) look-up on each page load, when the redirection is only required on each user's first page load.

try this code

robbin.zhao's picture

try this code
(ps: I didn't test it, but I hope it works :P)

backend bk {
  .host = "66.228.39.33";
  .port = "80";
  .connect_timeout = 30s;
  .first_byte_timeout = 45s;
  .between_bytes_timeout = 90s;
  .max_connections = 32;
}

acl asia {
    "111.11.111.111";
    "123.12.123.123";
    "456.123.12.72";
}

sub vcl_recv {
    # Block access from these ips
    if (client.ip ~ asia ) {
        set req.backend = bk;
        return (lookup);
    }
}

Drupal博客广州Drupal开发
Drupal大学Drupal大学

Thank you

pris54's picture

Thanks to btopro, Beanjammin and robbin.zhao for replying, your posts are very helpful.

P.

Paula Molinero

IT Solutions | pris54 Ltd
http://pris54.co.uk
info@pris54.co.uk

High performance

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: