Varnish Cache Plus

Geolocation (geoip)

Description

Converts IP addresses into country codes/name or region names. This is notably useful to implement geofencing.

This vmod uses libgeoip as a resolver, and your Linux distribution should package a free database with the libgeoip database.

Example VCL

vcl 4.0;
import geoip;

backend default { .host = "192.0.2.11"; .port = "8080"; }

sub vcl_recv {
        set req.http.X-Country-Name = geoip.country_name(client.ip);

        if (req.http.X-Country-Name != "Germany" ||
	    req.http.X-Country-Name != "Belgium") {
            return (synth(403, "Sorry, only available in Germany and Belgium"));
        }
}

API

country_code

STRING country_code(STRING)

Given a IP string, return the corresponding two-letter code of the relevant country.

country_name

STRING country_name(STRING)

Same as country_code, but returns the english country name instead of its code.

region_name

STRING region_name(STRING)

Return the name of the region associated with the input IP.

Availability

vmod_geoip is available in the varnish-plus-vmods-extra package.