In this tutorial, we will build a site displaying the visitor IP address and geolocation by leveraging the power of nginx GeoIP and Substitution modules. If you are using Debian stable, the default nginx package have these modules compiled-in. In case you are using backports, then installing either nginx-full or nginx-extras will do.
We start by fetching and unpacking the GeoIP Country and City databases :
mkdir -p /usr/share/GeoIP cd /usr/share/GeoIP wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz gunzip *gz
We now edit nginx.conf to add directives specifying the path to the GeoIP database files, within the http block :
We then edit the site configuration file to add the following content within the location block :
The instructions above are pretty much self-explaining : we start by setting the $location and $map variables, and we then use the sub_filter command to insert the generated content within nginx response.
Finally, we create a simple index.html file (note the IP_ADDRESS_PLACEHOLDER text which will get dynamically replaced) :
Let’s relaunch nginx and point our browser to our newly created site : if everything goes well, we should be greeted with informations about our IP address, geolocation, and a map.
A slightly more elaborate example is available here.