Updated to “GitHub - david-garcia-garcia/traefik-geoblock: Traefik Geoblock Plugin”
Which is much efficient than the older plugin.
First, let’s understand why you might want geoblocking:
- Restrict access to specific countries
- Prevent unauthorized access from high-risk regions
- Comply with regional regulations
- Reduce potential attack surface
Here’s a step-by-step guide to implement geoblocking:
- First, modify your
traefik_config.ymlto add the GeoBlock plugin. Add this under the experimental plugins section:
experimental:
plugins:
# ... existing plugins ...
geoblock:
moduleName: "github.com/david-garcia-garcia/traefik-geoblock"
version: "v1.0.1"
- In your
dynamic_config.yml, add the GeoBlock middleware configuration. You can add this under the middlewares section:
pangolin-geoblock:
plugin:
geoblock:
enabled: true
defaultAllow: false
databaseFilePath: "/plugins-storage/IP2LOCATION-LITE-DB1.IPV6.BIN"
allowPrivate: true
logBannedRequests: true
banIfError: true
disallowedStatusCode: 403
allowedCountries:
- US # United States
- CA # Canada
- GB # United Kingdom
- IN # India
# Add more countries as needed from the ISO 3166-1 alpha-2 codes
allowedIPBlocks:
- "192.168.0.0/16"
- "10.0.0.0/8"
bypassHeaders:
X-Internal-Request: "true"
X-Skip-Geoblock: "1"
- Apply the middleware to your enterypoints in
traefik_config.yml. You can add it to specific paths:
entryPoints:
web:
address: ":80"
websecure:
address: ":443"
transport:
respondingTimeouts:
readTimeout: "30m"
http:
middlewares:
- pangolin-geoblock@file
- Set up the IP2Location database by mounting it to your Traefik container. Add this to your Docker Compose file:
services:
traefik:
# ... existing config ...
volumes:
# ... existing volumes ...
- ./IP2LOCATION-LITE-DB1.IPV6.BIN:/plugins-storage/IP2LOCATION-LITE-DB1.IPV6.BIN
- Restart your Traefik container to apply the changes:
docker compose restart traefik
Important Configuration Options:
-
defaultAllow:false: Block by default, only allow listed countries (whitelist mode)true: Allow by default, only block listed countries (blacklist mode)
-
allowPrivate: Set totrueif you want to allow local network requests -
allowedCountriesorblockedCountries: List of two-letter ISO country codes to allow or block -
allowedIPBlocksorblockedIPBlocks: CIDR ranges to always allow or block -
bypassHeaders: Headers that will skip geoblocking entirely -
banIfError: Block requests if IP lookup fails -
databaseAutoUpdate: Enable automatic database updatesdatabaseAutoUpdate: true databaseAutoUpdateDir: "/data/ip2database" databaseAutoUpdateToken: "" # For premium IP2Location databaseAutoUpdateCode: "DB1"
Monitoring and Troubleshooting:
- Check Traefik logs for geoblocking activity:
docker compose logs -f traefik
- Enable detailed logging options in the configuration for debugging:
logLevel: "debug"
logFormat: "json"
logPath: "/var/log/geoblock.log" # Empty for Traefik's standard output
logBannedRequests: true
- Monitor the JSON logs which include fields like:
- IP address that triggered the action
- Country code
- Request host and method
- Processing phase where the action occurred
Security Considerations:
- Always use the middleware with HTTPS (websecure entrypoint)
- Consider implementing rate limiting alongside geoblocking
- Use allowPrivate carefully based on your needs
- Whitelist domains [“download.ip2location.com”, “www.ip2location.com”] if using auto-updates
Remember to update your countries list based on your specific needs.