Skip to content
Federico G. Schwindt edited this page Aug 9, 2018 · 27 revisions

Welcome to the libvmod-geoip2 wiki!

Examples

Please note you need to supply the full path to the mmdb files below, e.g. /var/db/maxmind/GeoLite2-Country.mmdb.

Pass the ISO code to the backend servers

import geoip2;

sub vcl_init {
    new country = geoip2.geoip2("GeoLite2-Country.mmdb");
}

sub vcl_recv {
    set req.http.X-Country-Code = country.lookup("country/iso_code", client.ip);
}	

As previous but using the first X-Forwarded-For IP if supplied

import geoip2;

sub vcl_init {
    new country = geoip2.geoip2("GeoLite2-Country.mmdb");
}

sub vcl_recv {
    set req.http.X-Country-Code = country.lookup("country/iso_code", 
        std.ip(regsub(req.http.X-Forwarded-For, "^(\d+\.\d+\.\d+\.\d+).*", "\1"), 
            client.ip));
}

Pass the city and state to the backend servers

import geoip2;

sub vcl_init {
    new country = geoip2.geoip2("GeoLite2-City.mmdb");
}

sub vcl_recv {
    set req.http.X-City-Name = country.lookup("city/names/en", client.ip);
    set req.http.X-State-Name = country.lookup("subdivisions/0/names/en", client.ip);
}	

For more information see mmdblookup.

Clone this wiki locally