brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: Nginx Proxy Manager ships the geoip2 module, so you can allow only Dutch visitors

Nginx
~/tips/tip-nginxproxymanager-geoip2-country

load_module /usr/lib/nginx/modules/ngx_http_geoip2_module.so;

Blocks visitors from outside the countries you choose on selected Nginx Proxy Manager hosts.

Plenty of things I host are only ever used by people in the Netherlands. A login page that the rest of the world never needs to see is a fair candidate for a country filter, and Nginx Proxy Manager already has the module for it on board.

NPM’s advanced configuration page says the geoip2 module is in the image and only needs loading. Create /data/nginx/custom/root_top.conf with:

load_module /usr/lib/nginx/modules/ngx_http_geoip2_module.so;
load_module /usr/lib/nginx/modules/ngx_stream_geoip2_module.so;

The module needs a MaxMind database. The free GeoLite2 Country file needs a MaxMind account to download. Put the .mmdb file in the data volume, then add the lookup and a map to /data/nginx/custom/http_top.conf:

geoip2 /data/geoip/GeoLite2-Country.mmdb {
    auto_reload 24h;
    $geoip2_country_code country iso_code;
}
map $geoip2_country_code $allowed_country {
    default no;
    NL yes;
}

Now the proxy hosts you want to lock get two lines in their Advanced tab:

if ($allowed_country = no) {
    return 403;
}

I’d do it per host and not globally, so a service that a friend abroad uses keeps working. Add BE yes; or other codes to the map when you need them.

Now the caveats. The lookup uses the client address nginx sees. With Cloudflare or another proxy in front, that is the proxy’s address, so you need the real IP setup first or every visitor counts as wherever that proxy sits. Your own LAN addresses, like 192.168.1.x, aren’t in the database either, so they get an empty code and hit default no. The module’s default=NL option on the variable is a quick way around that, at the price that every address missing from the database counts as Dutch too. The database also goes stale; auto_reload only rereads the file, updating it is up to you. And a country filter stops scanners, not a determined person with a Dutch VPN exit.

Further reading: Nginx Proxy Manager: Advanced Configuration (geoip2), ngx_http_geoip2_module README

Going deeper: Guide: fix a 502 Bad Gateway in Nginx Proxy Manager, with a checklist that finds the cause

More Nginx tips sudo certbot --nginx -d example.com -d www.example.com /data/nginx/custom/server_redirect.conf map $http_upgrade $connection_upgrade { proxy_intercept_errors on; proxy_redirect http://localhost:3000/ /app/; All Nginx tips (50) All topics

Link