Skip to content

Instantly share code, notes, and snippets.

@VirtuBox
Last active January 24, 2024 08:44
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save VirtuBox/9ed03c9bd9169202c358a8be181b7840 to your computer and use it in GitHub Desktop.
Save VirtuBox/9ed03c9bd9169202c358a8be181b7840 to your computer and use it in GitHub Desktop.
How to configure GeoIP module for Nginx

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

@cleitoncsl
Copy link

@Rar9
Copy link

Rar9 commented Jun 26, 2019

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.

@dallascao
Copy link

This project updates ip database on weekly bases but it's not working with nginx.

https://github.com/v2fly/geoip

Maybe it can be converted for nginx use.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment