Traefik plugin that registers a custom middleware for getting data from MaxMind GeoIP databases and pass it downstream via HTTP request headers.
Highly dynamically configurable, with ASN, City and Country data
Supports both GeoIP2 and GeoLite2 databases.
Simple example to run in docker compose with auto download maxmind database
docker-compose.yaml
services:traefik:image: "traefik:v3.2"container_name: "traefik"restart: unless-stoppedcommand:#- "--log.level=DEBUG"- "--api.insecure=true"- "--providers.docker=true"- "--providers.docker.exposedbydefault=false"- "--entryPoints.web.address=:80"- "--experimental.localPlugins.traefikgeoip.moduleName=github.com/thiagotognoli/traefikgeoip"ports:- "80:80"- "8080:8080"volumes:- "/var/run/docker.sock:/var/run/docker.sock:ro"- "../.:/plugins-local/src/github.com/thiagotognoli/traefikgeoip"- geoipupdate_data:/usr/share/GeoIPnetworks:- traefikgeoip_examplewhoami:image: "traefik/whoami"container_name: "traefik-whoami"restart: unless-stoppedlabels:- "traefik.enable=true"- "traefik.http.routers.whoami.rule=Host(`localhost`)"- "traefik.http.routers.whoami.entrypoints=web"- "traefik.http.middlewares.traefikgeoip.plugin.traefikgeoip.cityDbPath=/usr/share/GeoIP/GeoLite2-City.mmdb"- "traefik.http.middlewares.traefikgeoip.plugin.traefikgeoip.asnDbPath=/usr/share/GeoIP/GeoLite2-ASN.mmdb"- "traefik.http.middlewares.traefikgeoip.plugin.traefikgeoip.lightMode=true"- "traefik.http.middlewares.traefikgeoip.plugin.traefikgeoip.ipHeader=X-IP"- "traefik.http.middlewares.traefikgeoip.plugin.traefikgeoip.iso88591=true"# - "traefik.http.middlewares.traefikgeoip.plugin.traefikgeoip.preferXForwardedForHeader=true"# - "traefik.http.middlewares.traefikgeoip.plugin.traefikgeoip.failInError=true"- "traefik.http.routers.whoami.middlewares=traefikgeoip"networks:- traefikgeoip_examplegeoipupdate:container_name: cii_geoipupdateimage: ghcr.io/maxmind/geoipupdaterestart: unless-stopped# restart: on-failureenv_file:- ./maxmind.envvolumes:- geoipupdate_data:/usr/share/GeoIPnetworks:- traefikgeoip_examplevolumes:geoipupdate_data:networks:traefikgeoip_example:name: traefikgeoip_exampledriver: bridge
Request your free maxmind credentials in site and up in
maxmind.env
GEOIPUPDATE_ACCOUNT_ID=6025842GEOIPUPDATE_LICENSE_KEY=abQ2rY_UMmru2Gd6LrbfGcrmKFwvR1duEDPZ_nmwGEOIPUPDATE_EDITION_IDS=GeoLite2-ASN GeoLite2-City GeoLite2-CountryGEOIPUPDATE_FREQUENCY=72
The tricky part of installing this plugin into containerized environments, like Kubernetes, is that a container should contain a database within it.
[!WARNING] Setup below is provided for demonstration purpose and should not be used on production. Traefik's plugin site is observed to be frequently unavailable, so plugin download may fail on pod restart.
Tested with official Traefik chart version 26.0.0.
The following snippet should be added to values.yaml
:
experimental:plugins:geoip2:moduleName: github.com/thiagotognoli/traefikgeoipversion: v0.22.0deployment:additionalVolumes:- name: geoip2emptyDir: {}initContainers:- name: downloadimage: alpinevolumeMounts:- name: geoip2mountPath: /tmp/geoip2command:- "/bin/sh"- "-ce"- |wget -P /tmp https://raw.githubusercontent.com/thiagotognoli/traefikgeoip/main/geolite2.tgztar --directory /tmp/geoip2 -xvzf /tmp/geolite2.tgzadditionalVolumeMounts:- name: geoip2mountPath: /geoip2
apiVersion: traefik.io/v1alpha1kind: Middlewaremetadata:name: geoip2namespace: traefikspec:plugin:geoip2:dbPath: "/geoip2/GeoLite2-City.mmdb"
The plugin currently supports the following configuration settings:
Name | Description |
---|---|
cityDbPath | Container path to City GeoIP database. |
countryDbPath | Container path to Country GeoIP database. |
asnDbPath | Container path to ASN GeoIP database. |
preferXForwardedForHeader | Should X-Forwarded-For header be used to extract IP address. Default false . |
ipHeader | Alternate Header of IP. Default "" . |
failInError | Not start plugin in error. Default false . |
debug | Debug messages: false. Default false . |
iso88591 | Encode in ISO-8859-1; Default: false . |
Install Go, golangci-lint, yaegi and just
brew install go golangci-lint justgo install github.com/traefik/yaegi/cmd/yaegi@latest
To run linter and tests execute this command
just test
This project is a fork of https://github.com/traefik-plugins/traefikgeoip2
and some parts of https://github.com/Maronato/traefik_geoip