Create a folder to store the databases :
mkdir -p /usr/share/GeoIP
Download Country IP database
wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
gunzip GeoIP.dat.gz
mv GeoIP.dat /usr/share/GeoIP/GeoIP.dat
Download City IP database
wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
gunzip GeoLiteCity.dat.gz
mv GeoLiteCity.dat /usr/share/GeoIP/GeoIPCity.dat
Add the following line to your nginx.conf file :
geoip_country /usr/share/GeoIP/GeoIP.dat;
geoip_city /usr/share/GeoIP/GeoIPCity.dat;
And the following lines to your /etc/nginx/fastcgi_params
fastcgi_param GEOIP_ADDR $remote_addr;
fastcgi_param GEOIP_COUNTRY_CODE $geoip_country_code;
fastcgi_param GEOIP_COUNTRY_NAME $geoip_country_name;
fastcgi_param GEOIP_REGION $geoip_region;
fastcgi_param GEOIP_REGION_NAME $geoip_region_name;
fastcgi_param GEOIP_CITY $geoip_city;
fastcgi_param GEOIP_AREA_CODE $geoip_area_code;
fastcgi_param GEOIP_LATITUDE $geoip_latitude;
fastcgi_param GEOIP_LONGITUDE $geoip_longitude;
fastcgi_param GEOIP_POSTAL_CODE $geoip_postal_code;
Check if everything is okay with nginx -t
and then reload nginx service nginx reload
any update - I only found that this is not support anymore.
Discontinuation of the GeoLite Legacy Databases - https://blog.maxmind.com/2018/01/02/discontinuation-of-the-geolite-legacy-databases/
Would we need to update to verion 2?
If i follow https://guides.wp-bullet.com/blocking-country-and-continent-with-nginx-geoip-on-ubuntu-18-04/
and do sudo apt-get install geoip-database-extra libgeoip1 libnginx-mod-http-geoip -y
I get a error
Reading package lists... Done
Building dependency tree
Reading state information... Done
libgeoip1 is already the newest version (1.6.12-1).
geoip-database-extra is already the newest version (20180315-1).
libnginx-mod-http-geoip is already the newest version (1.14.0-0ubuntu1.2).
You might want to run 'apt --fix-broken install' to correct these.
The following packages have unmet dependencies:
libnginx-mod-http-geoip : Depends: nginx-common (= 1.14.0-0ubuntu1.2) but it is not going to be installed
E: Unmet dependencies. Try 'apt --fix-broken install' with no packages (or specify a solution).
Most probabel as nginx is in 1.17.x now.