Last active
February 14, 2024 22:32
-
-
Save kenjij/daadbc6c284fcd6c2b2d to your computer and use it in GitHub Desktop.
Downloading free MaxMind GeoIP file, use with NGINX
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Download the legacy format for NGINX compatibility | |
wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz | |
wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz | |
# Unzip | |
gunzip Geo*.gz | |
# Copy to /usr/share/GeoIP/ | |
cp Geo*.dat /usr/share/GeoIP/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Use GeoIP database | |
http { | |
geoip_country /usr/share/GeoIP/GeoIP.dat; | |
geoip_city /usr/share/GeoIP/GeoLiteCity.dat; | |
} | |
# Create mapping, then block | |
http { | |
map $geoip_country_code $allowed_country { | |
default yes; | |
CN no; | |
RU no; | |
} | |
} | |
server { | |
if ($allowed_country = no) { | |
return 444; | |
} | |
} | |
# Insert headers (reverse proxy case) | |
server { | |
proxy_set_header X_COUNTRY_CODE $geoip_country_code; | |
proxy_set_header X_CITY_COUNTRY_CODE $geoip_city_country_code; | |
proxy_set_header X_REGION $geoip_region; | |
proxy_set_header X_CITY $geoip_city; | |
proxy_set_header X_POSTAL_CODE $geoip_postal_code; | |
} |
Legacy GeoIP:
https://centminmod.com/centminmodparts/geoip-legacy/
GeoIPv2 nginx module:
https://github.com/leev/ngx_http_geoip2_module
Here https://github.com/sherpya/geolite2legacy is a tool to translate the new to the old format used by nginx,
and in the following link, there is a guy that makes it readily available https://www.miyuru.lk/geoiplegacy.
Thumbs up to https://twitter.com/miyurulk
Is there any way to detect the users using vpn/vps/hosting ips and block them?
Thanks for this note, you are saved my time :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Maxmind has discontinued their GeoIP database, in favor of the v2, which does not yet work with out-of-the-box nginx.
Therefore the above links does not work. Anyone have a link that works?