IPINFO.ZIP
Language: English

Use an MMDB with Nginx, Python and Node

Look up IP addresses locally, without an API call: in Nginx with the geoip2 module, in Python with maxminddb and in Node.js with maxmind.

01Pick a file

Every release has the full database and smaller files with one theme each. All of them are MaxMind DB (MMDB) files, so any MMDB library reads them.

  • ipinfo.mmdb.gz: everything per range: location, asn, company, abuse, privacy flags and threat data. About 4 GB once unpacked.
  • country.mmdb: country_code, country, continent_code.
  • privacy.mmdb: is_vpn, is_proxy, is_tor, is_relay, is_hosting and the VPN service.
  • asn-ip.mmdb: asn, as_name, as_domain, route.
  • abuse.mmdb: abuse_email, abuse_name, abuse_phone, network, asn.

The theme files are flat: their keys sit at the top of the record. In the full file the same values are nested, for example location.country_code, asn.asn or abuse.email. A flag that is false is left out of the record, so always read flags with a default.

download
mkdir -p /var/lib/ipinfo && cd /var/lib/ipinfo
curl -fsSLO https://ipinfo.zip/download/country.mmdb
curl -fsSLO https://ipinfo.zip/download/privacy.mmdb

A new release comes out every week. Download to a temporary name and rename it over the old file, so a reader never opens a half-written file.

update
curl -fsSL https://ipinfo.zip/download/privacy.mmdb -o privacy.mmdb.tmp && mv privacy.mmdb.tmp privacy.mmdb

02Nginx

Nginx reads MMDB files with the geoip2 module (ngx_http_geoip2_module). Debian and Ubuntu package it as libnginx-mod-http-geoip2, Alpine as nginx-mod-http-geoip2; both packages load the module for you.

Debian, Ubuntu
sudo apt install libnginx-mod-http-geoip2

In the http block, declare one variable per value. The words after the variable are the path in the record; default= is used when the address is not in the file. Booleans come out as 1.

/etc/nginx/conf.d/ipinfo.conf
geoip2 /var/lib/ipinfo/country.mmdb {
    auto_reload 1h;
    $ipz_country default=-- country_code;
}
geoip2 /var/lib/ipinfo/privacy.mmdb {
    auto_reload 1h;
    $ipz_vpn default=0 is_vpn;
    $ipz_tor default=0 is_tor;
}

server {
    listen 80;

    location / {
        # hand the values to the application
        proxy_set_header X-Country $ipz_country;
        proxy_set_header X-VPN     $ipz_vpn;
        proxy_pass http://127.0.0.1:3000;
    }

    location = /whereami {
        default_type text/plain;
        return 200 "$remote_addr $ipz_country vpn=$ipz_vpn tor=$ipz_tor\n";
    }
}

auto_reload makes Nginx pick up the new file after the weekly download, without a reload. The lookups use $remote_addr: behind a load balancer or a CDN, set the real client address first with the realip module (set_real_ip_from, real_ip_header).

With the full ipinfo.mmdb, give the nested path: $ipz_city location city; or $ipz_asn asn asn;.

check
$ curl http://localhost/whereami
89.187.164.1 US vpn=1 tor=0

03Python

install
pip install maxminddb
lookup.py
import maxminddb

# the full database: a nested record
with maxminddb.open_database('ipinfo.mmdb') as db:
    rec = db.get('89.187.164.1')
    if rec:
        print(rec['location']['country_code'], rec['asn']['asn'], rec['asn']['name'])
        print('VPN:', rec.get('is_vpn', False), rec.get('vpn', {}).get('service'))

# a theme file: a flat record, and the prefix length of its network
with maxminddb.open_database('privacy.mmdb') as db:
    rec, prefix_len = db.get_with_prefix_len('89.187.164.1')
    print(rec, prefix_len)
output
US 60068 Datacamp Limited (CDN77 / DataPacket)
VPN: True IVPN
{'is_vpn': True, 'is_hosting': True, 'is_anonymous': True, 'service': 'IVPN', 'vpn_type': 'interpolated'} 26

open_database memory-maps the file, so even the full database opens at once and uses little memory. get returns None for an address that is not in the file.

04Node.js

install
npm install maxmind
lookup.mjs
import maxmind from 'maxmind';

// reloads by itself when the weekly download replaces the file
const privacy = await maxmind.open('/var/lib/ipinfo/privacy.mmdb', { watchForUpdates: true });

const rec = privacy.get('89.187.164.1');
if (rec?.is_vpn) console.log('VPN:', rec.service); // VPN: IVPN

// Express: flag anonymous visitors for the routes that care
app.use((req, res, next) => {
  const r = privacy.get(req.ip);
  req.anonymous = Boolean(r?.is_vpn || r?.is_proxy || r?.is_tor);
  next();
});

The maxmind package reads the whole file into memory: fine for the theme files, about 4 GB for the full ipinfo.mmdb. On a small server, use the theme files, or Python, which maps the file instead. It refuses a .gz file: unpack it first with gunzip.

Behind a proxy, tell Express which hops to trust (app.set('trust proxy', 1)), or req.ip is the proxy's address.

05Check an answer

Compare a lookup with the report of the same address on the site, for example 89.187.164.1, or in a terminal: curl ipinfo.zip/89.187.164.1. Both come from the same release as the files.