This shows you the differences between two versions of the page.
— |
linux:nginx-geoip-block [2023/03/02 13:14] (current) admin created |
||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== GeoIP Block NGINX ====== | ||
+ | |||
+ | ===== Install Nginx modules ===== | ||
+ | |||
+ | |||
+ | To make use of the geographical filtering, we must first install the Nginx GeoIP module as well as the GeoIP database containing the mappings between visitors’ IP addresses and their respective countries. To do so, let’s execute: | ||
+ | |||
+ | <code bash> | ||
+ | $ sudo apt install libnginx-mod-http-geoip geoip-database | ||
+ | </code> | ||
+ | |||
+ | Download the GeoIP database. | ||
+ | |||
+ | <code bash> | ||
+ | $ sudo mkdir /usr/share/GeoIP | ||
+ | $ cd /usr/share/GeoIP | ||
+ | $ sudo wget sudo wget https://centminmod.com/centminmodparts/geoip-legacy/GeoIP.dat.gz | ||
+ | $ sudo gunzip GeoIP.dat.gz | ||
+ | </code> | ||
+ | |||
+ | Add config to nginx /etc/nginx/conf.d/geoip.conf: | ||
+ | |||
+ | <code bash> | ||
+ | # GeoIP database path | ||
+ | # | ||
+ | |||
+ | geoip_country /usr/share/GeoIP/GeoIP.dat; | ||
+ | </code> | ||
+ | |||
+ | Edit nginx config /etc/nginx/nginx.conf. Add this below the http { line to only allow Russian IPs You can use ISO’s full, searchable list of all country codes to find your code. | ||
+ | |||
+ | <code bash> | ||
+ | map $geoip_country_code $allowed_country { | ||
+ | default no; | ||
+ | RU yes; | ||
+ | } | ||
+ | </code> | ||
+ | |||
+ | Finally, add this to your sites virtual config /etc/nginx/sites-available/siteconfig below server {: | ||
+ | |||
+ | <code bash> | ||
+ | if ($allowed_country = no) {return 403; | ||
+ | } | ||
+ | </code> | ||
+ | |||
+ | And reload Nginx sudo systemctl restart nginx |