Nginx with GeoIP2 MaxMind Database to Fetch User Geolocation Data

Nginx with GeoIP2 MaxMind Database to Fetch User Geolocation Data

Overview

This is second part about fetching user geolocation data using Nginx and MaxMind Database. In our previous blog on Nginx with GeoIP MaxMind Database to Fetch User Geolocation Data, we discussed about fetching user geolocation data using Nginx and legacy (GeoIP) MaxMind Database.

In this blog, let us discuss about finding geographical location of a user using user’s IP address by just configuring Nginx and GeoIP2 MaxMind Databases and without doing any coding. GeoIP2 has many features such as localized name data, country, country subdivisions, FIPS 10-4, custom country codes, represented country, user type, net speed, registered country, ISO 3166-2, and autonomous system number. ngx_http_geoip2_module is used to create variables with values based on specific variable or client’s default IP.

Pre-requisites

  • Ubuntu Platform (Ubuntu 16.04, 12.04, 11.04)

Use Case

Install Nginx on Ubuntu, configure Nginx with MaxMind Databases, and find the geolocation of the user using IP address.

Synopsis

  • Installation and Configuration
  • Fetching Geolocation Data Using Client IP

Installation and Configuration

Downloading Nginx

Download Nginx version 1.12.0 using the below command:

Note: Choose Nginx version 1.9 or above to work with this sample.

Installing libmaxminddb

This library is used to read MaxMind DB files and GeoIP2 databases. It enables faster lookup of IP addresses of a client.

To install libmaxminddb, perform the following:

  • Add PPA to APT sources using the below command:
  • Install packages using the below command:

Building ngx_http_geoip2_module as Dynamic Module (Nginx 1.9.11+)

As ngx_http_geoip_module does not support GeoIP2, a third-party module – ngx_http_geoip2_module is used. It supports Nginx streams and GeoIP2.

To include ngx_http_geoip2_module into Nginx, perform the following:

  • Download ngx_http_geoip2_module from GitHub repository
objs/ngx_http_geoip2_module.so module will be automatically created. You can copy it to Nginx module path manually, if needed.

  • Add the following line to nginx.conf file:

Downloading Legacy MaxMind GeoCity and GeoCountry Databases

To download and extract MaxMind GeoCity and GeoCountry databases in Ubuntu system, use the following commands:

Testing MaxMind GeoCity and GeoCountry Databases Using mmdblookup

mmdblookup is used to look up geo information of an IP address from a MaxMind database file.

To test the MaxMind GeoCity and GeoCountry Databases, use the below command:

Configuring Nginx with MaxMind Databases

Nginx is configured with MaxMind GeoCity and GeoCountry Databases to access MaxMind geo variables.

To configure Nginx with the Databases, use the below command:

Fetching Geolocation Data Using Client IP

A sample web application (Node.js application) is created to return requested header parameters via JSON response. Custom geo fields are added to the requested header and are made accessible from the application. The application is reverse proxied via Nginx.

The output of the application with the geolocation data looks similar to the one as shown below:

select

Note: To run the sample Node.js application, Node.js should be installed with required modules.

The application log with the geolocation data looks similar to the one as shown below:

select

Conclusion

In this blog, we discussed about installing MaxMind GeoIP2 module with Nginx on Ubuntu and configuring Nginx with MaxMind GeoCity and GeoCountry Databases. We reverse proxied the sample web application through Nginx to find the geolocation of the user using IP address. For more details about legacy (GeoIP) MaxMind database model, refer our previous blog on Nginx with GeoIP MaxMind Database to Fetch User Geolocation Data.

References

556 Views 1 Views Today