IPIDB

Block countries in nginx with an MMDB database

Block or allow traffic by country in nginx with the geoip2 module and an MMDB country database: install, map country codes, exceptions and testing.

When you need to block or allow whole countries on a web server, looking each request up over an API adds latency. nginx can do it locally instead: the geoip2 module reads a country database in MMDB format into memory and sets a variable with the country code of every request. This guide installs the module, blocks a list of countries, adds exceptions, and shows how to test the rules.

What you need

  • nginx 1.18 or newer, with root access to the server.
  • A country database in MMDB format that stores the ISO code under country.iso_code. That is the common layout for country databases; for a licence to IPIDB data in bulk see database licences.

Install the module

On Debian and Ubuntu the module and the library are packaged:

Shell
sudo apt install libnginx-mod-http-geoip2 libmaxminddb0 mmdb-bin
sudo nginx -V 2>&1 | grep -o geoip2     # the module is loaded from /etc/nginx/modules-enabled

On other systems build ngx_http_geoip2_module as a dynamic module against your nginx version and load it with load_module modules/ngx_http_geoip2_module.so; at the top of nginx.conf.

Copy the database to /etc/nginx/geo/country.mmdb and check that it answers:

Shell
sudo mkdir -p /etc/nginx/geo
sudo cp country.mmdb /etc/nginx/geo/country.mmdb
mmdblookup --file /etc/nginx/geo/country.mmdb --ip 8.8.8.8 country iso_code

Map the country code

Put the following into the http block, for example in /etc/nginx/conf.d/geo.conf. The first part reads the code into $country_code; auto_reload picks up a new database file without restarting nginx. The map turns the code into a yes or no.

/etc/nginx/conf.d/geo.conf
geoip2 /etc/nginx/geo/country.mmdb {
    auto_reload 60m;
    $country_code default=XX country iso_code;
}

# 1 = blocked. Add or remove ISO 3166-1 alpha-2 codes.
map $country_code $blocked_country {
    default 0;
    KP      1;
    IR      1;
    SY      1;
}

Block in a server block

/etc/nginx/sites-available/example.conf
server {
    listen 443 ssl;
    server_name example.com;

    if ($blocked_country) {
        return 403;
    }

    location / {
        proxy_pass http://127.0.0.1:8080;
    }
}

if with a plain return is one of the safe uses of if in nginx. To show a page instead of a bare 403, add error_page 403 /blocked.html; and a location = /blocked.html that serves it.

Allow only some countries

For a service that should only be reached from a few countries, turn the map around: everything is blocked unless the code is listed. Keep XX (unknown) in mind; addresses that are not in the database, such as new ranges, would otherwise be locked out.

Allow list
map $country_code $blocked_country {
    default 1;
    HR      0;
    SI      0;
    AT      0;
    XX      0;   # unknown addresses are let through
}

Exceptions for your own addresses

Monitoring, payment providers and your office should never be blocked. A geo block matches addresses and ranges before the country check:

Exceptions
geo $geo_whitelist {
    default         0;
    203.0.113.10    1;   # office
    198.51.100.0/24 1;   # monitoring
}

map "$geo_whitelist:$blocked_country" $deny {
    default 0;
    "0:1"   1;
}

Then use if ($deny) { return 403; } in the server block instead of $blocked_country.

Behind Cloudflare or a load balancer

If nginx sits behind a proxy, $remote_addr is the proxy. Enable the real IP module for your proxy ranges so that the geoip2 lookup sees the visitor:

Real client address
set_real_ip_from 10.0.0.0/8;          # your load balancer
real_ip_header   X-Forwarded-For;
real_ip_recursive on;

For Cloudflare, list its published ranges with set_real_ip_from and use real_ip_header CF-Connecting-IP;.

Test and log

Shell
sudo nginx -t && sudo systemctl reload nginx
curl -I https://example.com/            # 200 from an allowed country

To see what nginx decides, add the country to the access log: log_format geo '$remote_addr $country_code $status "$request"'; and access_log /var/log/nginx/geo.log geo;. Check a few blocked entries against the IP lookup API before you rely on the rules.

Keep the database current

Address ranges move between networks and countries every week. Replace the MMDB file on a schedule; with auto_reload nginx loads the new file by itself. Write the new file next to the old one and rename it, so nginx never reads a half-copied database.

Related