Created
May 8, 2017 04:52
-
-
Save section-io-gists/d149b82bde66e66e454d9c1361e2682a to your computer and use it in GitHub Desktop.
Redirect traffic based on GEO IP lookup (Varnish 4)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sub vcl_recv { | |
# section.io Edge node automatically performs GEO IP (and city) lookup on every request. | |
# We create a request header called "section-io-geo-country" that you can leverage | |
if (req.http.section-io-geo-country ~ "^(AU|NZ)$") { | |
# Australia and New Zealand | |
#return (synth(802, "https://www.domain.com.au/")); | |
} else if (req.http.section-io-geo-country ~ "^(US|CA|MX)$") { | |
# US, Canada and Mexico | |
#return (synth(802, "http://www.domain.com/")); | |
} else { | |
# UK and the rest of the world | |
#return (synth(802, "http://www.domain.co.uk")); | |
} | |
} | |
sub vcl_synth { | |
if (resp.status == 802) { | |
set resp.http.Location = resp.reason; | |
set resp.status = 302; | |
return (deliver); | |
} | |
} | |
#Ideas to extend the above: | |
#1) Check for a cookie value to override the logic in vcl_recv (and set a cookie value when a user chooses a specific site/country to browse to) | |
#2) When redirecting onto a site with GEO IP logic look for a query string such as"redirect=" and | |
#override redirect logic in vcl_recv as this user has been redirected onto the site |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment