Skip to content
Federico G. Schwindt edited this page May 28, 2023 · 27 revisions

Packages

Third-party packages are available for CentOS / RedHat 7.
Installation instructions for Varnish 6.0 LTS are provided below.

Varnish 6.0

yum install https://extras.getpagespeed.com/release-el7-latest.rpm yum-utils
yum-config-manager --enable getpagespeed-extras-varnish60
yum install vmod-geoip2

Database details

GeoIP2 databases can be inspected using mmdblookup(1).

For example running mmdblookup --file GeoLite2-City.mmdb --ip 1.2.3.4 will display:

  {
    "continent": 
      {
        "code": 
          "NA" <utf8_string>
        "geoname_id": 
          6255149 <uint32>
        "names": 
          {
            "de": 
              "Nordamerika" <utf8_string>
            "en": 
              "North America" <utf8_string>
            "es": 
              "Norteamérica" <utf8_string>
            "fr": 
              "Amérique du Nord" <utf8_string>
            "ja": 
              "北アメリカ" <utf8_string>
            "pt-BR": 
              "América do Norte" <utf8_string>
            "ru": 
              "Северная Америка" <utf8_string>
            "zh-CN": 
              "北美洲" <utf8_string>
          }
      }
    "country": 
      {
        "geoname_id": 
          6252001 <uint32>
        "iso_code": 
          "US" <utf8_string>
        "names": 
          {
            "de": 
              "USA" <utf8_string>
            "en": 
              "United States" <utf8_string>
            "es": 
              "Estados Unidos" <utf8_string>
            "fr": 
              "États-Unis" <utf8_string>
            "ja": 
              "アメリカ合衆国" <utf8_string>
            "pt-BR": 
              "Estados Unidos" <utf8_string>
            "ru": 
              "США" <utf8_string>
            "zh-CN": 
              "美国" <utf8_string>
          }
      }
    "location": 
      {
        "accuracy_radius": 
          1000 <uint16>
        "latitude": 
          37.751000 <double>
        "longitude": 
          -97.822000 <double>
      }
    "registered_country": 
      {
        "geoname_id": 
          2077456 <uint32>
        "iso_code": 
          "AU" <utf8_string>
        "names": 
          {
            "de": 
              "Australien" <utf8_string>
            "en": 
              "Australia" <utf8_string>
            "es": 
              "Australia" <utf8_string>
            "fr": 
              "Australie" <utf8_string>
            "ja": 
              "オーストラリア" <utf8_string>
            "pt-BR": 
              "Austrália" <utf8_string>
            "ru": 
              "Австралия" <utf8_string>
            "zh-CN": 
              "澳大利亚" <utf8_string>
          }
      }
  }

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 country ISO code to the backend servers

import geoip2;

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

sub vcl_recv {
    set req.http.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.Country-Code = country.lookup("country/iso_code", 
        std.ip(regsub(req.http.X-Forwarded-For, "[\s,].*", ""), 
            client.ip));
}

Pass the city and state to the backend servers

import geoip2;

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

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