Using GeoIP Functionality in Varnish

Ken Brownfield kb+varnish at slide.com
Tue Mar 16 20:21:13 CET 2010


I think what would work for you is to add only the C{} block from the top of the plugin.vcl to the top of your VCL, and do something like the following where you need to (vcl_recv() presumably):

C{
	if (get_country_code) {
		char *country = (*get_country_code)(VRT_IP_string(sp, VRT_r_server_ip(sp)));
		if (!strncmp(country, "US", 2))
			VRT_l_req_url(sp, "/us/", vrt_magic_string_end);
	}
}C

Note that GeoIP is only accessible via C{} blocks, not VCL directly.

Caveats:
 * No guarantees ;)
 * You can remove the extra "char *country" step if you want; I did it for readability
 * Assumes replacing the URL with "/us/" instead of prepending to the existing URL
 * The GeoIP plugin on the twiki could use some love; it seems overly complicated and limited.
-- 
kb

On Mar 16, 2010, at 9:22 AM, Sebastian Cabrera wrote:

> Hi everybody,
> 
> On a Website i want to have the posibility to do some rewriting due to
> the Country he's comming from. So if someone visits the Mainpage
> (domain.com) from US i want to have him visiting domain.com/us/.
> Does anyone have any experiences with that? How can i do this?
> I just downloaded and installed the GeoIP Plugin from the varnish wiki
> (http://varnish-cache.org/wiki/GeoipUsingInlineC), but how can i use it?
> Is there any possibility to do something like
> 
> if (get_country_code(client.ip) == "US") {
>    set req.url = "/us/"
> }
> 
> ?
> 
> thanks for your help,
> regards,
> 
> Sebastian





More information about the varnish-misc mailing list