GeoIP blocking at the edge can cut a lot of credential-stuffing and scraping traffic from countries that have no business reaching your app. Traefik has no built-in geo middleware. A community plugin can read a MaxMind GeoLite2 database, look up each client IP, and refuse requests from countries you don’t serve. This article covers the plugin install, keeping the database fresh, getting the real client IP, and the legal and operational pitfalls.
How to verify
# Set TEST_IP to a public address in a country you deny, then check what the database says
mmdblookup --file /opt/traefik/geoip/GeoLite2-Country.mmdb --ip "$TEST_IP" country iso_code
# From a host inside both trusted ranges, send that address as the client; expect 403
curl -s -o /dev/null -w '%{http_code}\n' -H "X-Forwarded-For: $TEST_IP" https://api.example.com/
# The backend access log should show NO entry for the blocked request
# A normal request from an allowed country should get your usual status code
curl -s -o /dev/null -w '%{http_code}\n' https://api.example.com/
# With debug: true and Traefik's log level at DEBUG, each decision is logged
docker logs traefik 2>&1 | grep maxmind-geoblock | tail -5
# Confirm the database is current: the date should be under a month old
docker exec traefik ls -l /etc/traefik/geoip/GeoLite2-Country.mmdb
mmdblookup comes from MaxMind’s libmaxminddb (the mmdb-bin package on Debian and Ubuntu).
What’s happening
MaxMind publishes the GeoLite2 country and city databases. Each is a binary .mmdb file that maps IP ranges to country, city or ASN (autonomous system number, the network operator) data. The free GeoLite2 tier is good enough for country-level blocking. The paid GeoIP2 tier is more accurate, especially for mobile and carrier-NAT ranges, and is what you should use if blocking decisions affect revenue.
Traefik does not read .mmdb files natively, so you add a plugin from the Traefik Plugin Catalog. Plugins run inside Yaegi, the Go interpreter built into Traefik. There are two kinds. Enrichment plugins such as traefikgeoip2 add headers like X-GeoIP2-Country for your backend but never block. Blocking plugins return an error themselves. This article uses maxmind-geoblock, which reads the same GeoLite2 file and answers 403 to countries you exclude. It is a young project (first release in October 2026), so pin the version and test each upgrade.
Why not pair an enrichment plugin with a router rule? Traefik matches the router rule first, and only then runs the router’s middlewares. A Header() or HeaderRegexp() rule never sees a header that a middleware adds later, so that pairing can’t block anything.
The client IP detection is the load-bearing detail. Behind a CDN or load balancer, the immediate peer is the CDN, not the end user. Traefik only keeps X-Forwarded-For from peers listed in forwardedHeaders.trustedIPs. The plugin has its own list, trustedForwarders, and ignores the header from anyone else. The ipStrategy.depth option you may know from Traefik’s built-in IPAllowList middleware doesn’t apply to plugins. Set both lists to the same upstream ranges, or every block decision is made on the wrong address.
Keeping the database current is an operational task. Country ranges shift and new allocations appear, so a stale file starts blocking people who used to get through. The GeoLite licence also requires you to delete a GeoLite database within 30 days of a new release. MaxMind’s own example schedules the update twice a week, and GeoLite accounts are limited to 30 downloads per day. Automate the refresh.
The procedure
-
Create a free MaxMind account, generate a license key, and download the country database. MaxMind’s download links use your account ID and license key as HTTP Basic credentials.
sudo mkdir -p /opt/traefik/geoip curl -sSL -u "${MAXMIND_ACCOUNT_ID}:${MAXMIND_LICENSE_KEY}" \ "https://download.maxmind.com/geoip/databases/GeoLite2-Country/download?suffix=tar.gz" \ -o /tmp/geoip.tar.gz tar -xzf /tmp/geoip.tar.gz -C /tmp sudo cp /tmp/GeoLite2-Country_*/GeoLite2-Country.mmdb /opt/traefik/geoip/ -
Mount the directory into Traefik and register the plugin in the static configuration.
# docker-compose.yml services: traefik: image: traefik:v3.6 volumes: - /opt/traefik/geoip:/etc/traefik/geoip:ro# static config experimental: plugins: maxmind-geoblock: moduleName: github.com/gabay/maxmind-geoblock version: v0.2.0Traefik downloads the plugin from the catalog at startup, so the host needs outbound HTTPS the first time.
-
Configure the middleware in the dynamic configuration. This example is an allow-list: only the listed countries get through.
http: middlewares: geoblock: plugin: maxmind-geoblock: dbPath: /etc/traefik/geoip/GeoLite2-Country.mmdb allowedCountries: [CA, US, FR, GB, DE] allowedIPs: - 192.0.2.10 trustedForwarders: - 10.0.0.0/8 - 172.16.0.0/12 allowUnknown: false blockStatusCode: 403To block a few countries and allow the rest, replace
allowedCountrieswithdeniedCountries: [CN, RU, KP, IR]. The plugin refusesallowedCountriescombined with any other country or continent list. UseallowedIPsfor monitoring probes or an office address that must always pass.allowUnknown: falseis fail-closed. An IP with no country in the database is blocked, and so is every request if the file is missing or corrupt (onlyallowedIPsstill pass). SetallowUnknown: truefor fail-open if an outage of the filter is worse for you than a gap in it. -
Attach the middleware to the router.
http: routers: api: rule: "Host(`api.example.com`)" entryPoints: [websecure] service: api-backend middlewares: [geoblock] -
Set the trust boundary for X-Forwarded-For. If Traefik sits directly on the internet, the client IP is the peer, and you can leave both trusted lists empty. If it sits behind a CDN or load balancer, set
forwardedHeaders.trustedIPsto the upstream’s IP range, and use the same ranges in the plugin’strustedForwarders.entryPoints: websecure: address: ":443" forwardedHeaders: trustedIPs: - "10.0.0.0/8" - "172.16.0.0/12" -
Automate the database refresh with a cron job or systemd timer that runs twice a week. The plugin checks the file every
dbReloadInterval(one minute by default) and reloads it when it changes, so Traefik needs no restart. Writing to a temporary name and renaming it means the plugin never reads a half-copied file.#!/usr/bin/env bash # /usr/local/sbin/refresh-geoip.sh set -euo pipefail # MAXMIND_ACCOUNT_ID and MAXMIND_LICENSE_KEY come from a root-only file . /etc/default/refresh-geoip work=$(mktemp -d) trap 'rm -rf "$work"' EXIT curl -sSL -u "${MAXMIND_ACCOUNT_ID}:${MAXMIND_LICENSE_KEY}" \ "https://download.maxmind.com/geoip/databases/GeoLite2-Country/download?suffix=tar.gz" \ -o "$work/g.tar.gz" tar -xzf "$work/g.tar.gz" -C "$work" install -o root -g root -m 0644 "$work"/GeoLite2-Country_*/GeoLite2-Country.mmdb /opt/traefik/geoip/GeoLite2-Country.mmdb.new mv -f /opt/traefik/geoip/GeoLite2-Country.mmdb.new /opt/traefik/geoip/GeoLite2-Country.mmdb
Common pitfalls
- Blocking a country your customers actually use. Check the allow-list against real traffic before you switch it on. An audit query on the access log for non-allowed countries shows the noise before you block real users.
- Using GeoLite2 (free) for revenue-affecting blocks. The free tier has known accuracy gaps for mobile IP ranges, so pay for GeoIP2 if it matters.
- Locking everyone out with fail-closed. With
allowUnknown: false, a missing or unreadable database blocks every request exceptallowedIPs. Watch Traefik’s error log for database load failures, and keep your monitoring probe inallowedIPs. - Forgetting that Cloudflare can add a
CF-IPCountryheader. AHeaderRegexp()router rule can match it, because it arrives with the request. Only trust it if your origin accepts traffic from Cloudflare’s IP ranges alone, since anyone else can send the same header. - A trust list that is too wide. If
trustedForwardersandtrustedIPsinclude networks you don’t control, any client there can sendX-Forwarded-For: 1.2.3.4and pick its own country. Keep both lists to your real upstream ranges. - Legal exposure. GDPR, CCPA and similar regimes do not require geo-blocking. Some clients do, such as those bound by the US Treasury OFAC sanctions list or in export-controlled industries. Treat the block-list as a compliance record and audit it.
GeoIP blocking is a coarse but effective filter. The operational discipline (a fresh .mmdb, correct X-Forwarded-For trust, and an allow-list reviewed quarterly) is what keeps it useful instead of disruptive. That review cycle is part of how Stack Harbor handles managed operations for clients with abuse-prone edges. For IP-level allow-listing (rather than geo-level), see traefik-ip-whitelist. MaxMind’s updating databases page covers geoipupdate, its official update tool, if you’d rather not script the download yourself.