Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Redirect traffic based on GEO IP lookup with whitelist (Varnish 4)
acl whitelist_geo {
"13.13.22.30";
"123.39.96.0"/24;
}
acl geo_to_au {
"1.1.22.30";
"133.29.93.0"/30;
}
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
# Force IP addresses from ACL to AU
if((std.ip(req.http.True-Client-IP, "0.0.0.0") ~ geo_to_au) {
# Australia and New Zealand
#return (synth(802, "https://www.domain.com.au/"));
}
# Only GEO redirect if IP address is not in whitelist ACL
if(!(std.ip(req.http.True-Client-IP, "0.0.0.0") ~ whitelist_geo) {
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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment