Mémo pour configurer NGINX et GeoIP pour blacklister un pays et lui refuser l’accès à un site web.
GeoIP de Maxmind permet d’identifier la localisation via l’adresse ip d’un visiteur.
Il suffit alors de configurer NGINX pour refuser ou accepter cette ip.
Vérifiez que NGINX possède le module http_geoip chargé :
1 |
command nginx -V |
Résultat :
1 2 3 |
nginx version: nginx/1.2.1 TLS SNI support enabled configure arguments: --prefix=/etc/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-log-path=/var/log/nginx/access.log --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --lock-path=/var/lock/nginx.lock --pid-path=/var/run/nginx.pid --with-pcre-jit --with-debug --with-http_addition_module --with-http_dav_module --with-http_geoip_module --with-http_gzip_static_module --with-http_image_filter_module --with-http_realip_module --with-http_stub_status_module --with-http_ssl_module --with-http_sub_module --with-http_xslt_module --with-ipv6 --with-sha1=/usr/include/openssl --with-md5=/usr/include/openssl --with-mail --with-mail_ssl_module --add-module=/tmp/buildd/nginx-1.2.1/debian/modules/nginx-auth-pam --add-module=/tmp/buildd/nginx-1.2.1/debian/modules/nginx-echo --add-module=/tmp/buildd/nginx-1.2.1/debian/modules/nginx-upstream-fair --add-module=/tmp/buildd/nginx-1.2.1/debian/modules/nginx-dav-ext-module |
Vous devez avoir « –with-http_geoip_module » dans la liste.
Installez geoip (Debian) :
1 |
command apt-get install geoip-database libgeoip1 |
Créez le fichier /etc/nginx/conf.d/geo.conf
1 2 3 4 5 6 |
geoip_country /usr/share/GeoIP/GeoIP.dat; map $geoip_country_code $allowed_country { default yes; CI no; FR no; } |
Dans l’exemple, la France et la Côte d’Ivoire sont refusées.
Dans le fichier de déclaration du site web :
1 2 3 4 5 6 7 8 9 10 |
location / { ... ... if ($allowed_country = no) { return 444; } ... ... ... } |
Testez et relancer NGINX :
1 2 |
command service nginx configtest command service nginx restart |
Au besoin vous pouvez mettre à jour manuellement la base de données :
1 2 3 |
command cd /usr/share/GeoIP/ command wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz command gunzip GeoIP.dat.gz |
Plus d’infos : http://wiki.nginx.org/HttpGeoipModule