Index ¦ Archives ¦ Atom

NGINX mit GeoIP2

Gleich mal vorweg, als das Internet noch laessig war, wollte man das was gleich erklaert wird eigentlich nie machen! Jetzt ist dies leider mehr oder weniger noetig, gerade wenn man "spannende" Applikationen am Netz hat.

Das Setup unter FreeBSD ist eigentlich ganz simpel, wenn man sich den nginx selber baut, dies setze ich voraus - das Modul "HTTP_GEOIP2" muss aktiviert werden, dies ist bei nginx aus dem FreeBSD Repo nicht der Fall.

Weiters braucht man das Paket "geoipupdate" und man kann eigentlich schon loslegen.

Nach der Registrierung und dem anlegen eines Keys bei MaxMind muss noch die Konfiguration nach /usr/local/share/geoipupdate/GeoIP.conf gespeichert werden:

# GeoIP.conf file for `geoipupdate` program, for versions >= 3.1.1.
# Used to update GeoIP databases from https://www.maxmind.com.
# For more information about this config file, visit the docs at
# https://dev.maxmind.com/geoip/geoipupdate/.
# `AccountID` is from your MaxMind account.
AccountID XXXXXXXXX
# `LicenseKey` is from your MaxMind account
LicenseKey XXXXXXXXXXXXXXXXXX
# `EditionIDs` is from your MaxMind account.
EditionIDs GeoLite2-ASN GeoLite2-City GeoLite2-Country

Anschliessend kann man sich die GeoIP Datenbank das erste Mal abholen:

/usr/local/bin/geoipupdate -f /usr/local/share/geoipupdate/GeoIP.conf

Dies empfehle ich regelmaessig via Cron zu erledigen...

Danach kann man in der http-Sektion der nginx.conf die entsprechenden Funktionen verwenden

#GeoIP
geoip2 /usr/local/share/GeoIP/GeoLite2-Country.mmdb {
auto_reload 60m;
$geoip2_metadata_country_build metadata build_epoch;
$geoip2_data_country_code country iso_code;
$geoip2_data_country_name country names en;
}
map $geoip2_data_country_code $allowed_country_general {
default no;
DE yes;
CH yes;
LI yes;
AT yes;
}

Ab jetzt kann in der server-Sektion das Geo-Blocking verwendet werden

location / {
    if ($allowed_country_general = no) {
     return 403;
    }
    proxy_pass http://xxx.xxx.xxx.xxx;
    include includes/proxy.conf;
}

Bitte immer beachten: Geo-Blocking schuetzt nur bedingt und ev. handelt man sich dabei sogar ganz andere Probleme ein. In Kombination mit anderen Massnahmen kann es aber ein kleiner Teil der Loesung sein.

© Christian Rhomberg. Built using Pelican. Theme by Giulio Fidente on github.