Geo-blocking with SWAG
SWAG can support MaxMind databases which are used to provide IP Geolocation and Online Fraud Prevention. It was initially tougher to setup but since end of 2021, it's now integrated through Linuxserver mods system. Let's dig into it!
We talked about SWAG, SWAG Dashboard and more recently how to integrate CrowdSec into SWAG, let's talk about how to do some Geo-blocking with SWAG.
SWAG can support MaxMind databases which are used to provide IP Geolocation and Online Fraud Prevention. It was initially tougher to setup but since end of 2021, it's now integrated through Linuxserver mods system, so let me guide you into it!
Setup the mod
- First of all, you need to create an account on MaxMind to get a free license key, mandatory to get access to their databases. Sign up to their GeoLite2 service, it's pretty straightforward
- When you have the key, modify your SWAG
docker-compose.yml
file to add the MaxMind mod through an environment variable like this:DOCKER_MODS=linuxserver/mods:swag-maxmind
If you already have mods, ensure they are separated by|
, such asDOCKER_MODS=linuxserver/mods:swag-dashboard|linuxserver/mods:swag-crowdsec|linuxserver/mods:swag-maxmind
- Add another environment variable
MAXMINDDB_LICENSE_KEY=<license-key>
and include your license key into it - Restart your container with
docker compose up -d
to apply the changes - Configure nginx to load the maxmind configuration file by adding the line below in the
http
section of/config/nginx/nginx.conf
.
The setup is now ready!
Manage whitelist and blocklist
You can manage your whitelist and blocklist per country by editing /config/nginx/maxmind.conf
with your favorite text editor, you have to enter the ISO code of the country followed by yes
or no
.
It includes an example for blocking high risk countries from GilbN's list based on the Spamhaus statistics and Akamai’s state of the internet report.
Apply to your definitions
To use your whitelist and blocklist, you need to include these few lines (line 9-10) in your SWAG definitions.
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name some-app.*;
include /config/nginx/ssl.conf;
client_max_body_size 0;
if ($lan-ip = yes) { set $geo-whitelist yes; }
if ($geo-whitelist = no) { return 404; }
location / {
And that's it 🚀! Restart the container to apply your configuration changes and any traffic coming for one of these countries is going to be automatically blocked!
PS: If you are using services like Cloudflare in front of your websites, it might not work as expected as your websites will be accessed by Cloudflare's IP addresses.