Installing the module

First, check if your build of nginx is already using the GeoIP module. Run the following command:

nginx -V

If the output contains

--with-http_geoip_module

then you don’t have to do anything to install it. You just have to configure nginx to use it.

If it doesn’t contain

--with-http_geoip_module

then you’ll have to build nginx yourself, passing

--with-http_geoip_module

to

./configure

. For more information on building nginx, see this.

Configuring the module

Open your server configuration file and add the following to the http section:

geoip_country  /path/to/CountryGeoIP.dat
geoip_city     /path/to/GeoIPOrLiteCity.dat;

Replace

/path/to/CountryGeoIP.dat

with the path to your GeoIP country database and replace

/path/to/GeoIPOrLiteCity.dat

with the path to your GeoIP city database.

IMPORTANT: For the nginx module, you must provide both a country database and city database. If you just provide a city database, Piwik will not see any country information.

If you’re using an Organization or ISP database, you can add it to the config with:

geoip_org /path/to/GeoIPISPOrOrg.dat

Unfortunately, you can only supply one database here for the nginx module, so if you have both the ISP and Organization database, you will have to pick one to use.

Now make sure the following parameters are passed to fastcgi:

fastcgi_param GEOIP_ADDR $remote_addr;
fastcgi_param GEOIP_COUNTRY_CODE $geoip_country_code;
fastcgi_param GEOIP_COUNTRY_NAME $geoip_country_name;

If you’re using a city database, add the following parameters:

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;

If you’re using an organization database, add the following parameters:

fastcgi_param GEOIP_ORGANIZATION $geoip_org;

That’s all, folks. Enjoy!