Skip to content

Instantly share code, notes, and snippets.

@heartshare
Forked from kenjij/geoip.sh
Created October 19, 2022 08:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heartshare/80d750f89f03a1b0452dfe46504d0979 to your computer and use it in GitHub Desktop.
Save heartshare/80d750f89f03a1b0452dfe46504d0979 to your computer and use it in GitHub Desktop.
Downloading free MaxMind GeoIP file, use with NGINX
# 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/
# 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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment