Nginx with GeoIP MaxMind Database to Fetch User Geolocation Data

Nginx with GeoIP MaxMind Database to Fetch User Geolocation Data

Overview

Geolocation data of a user plays a significant role in business marketing. This data is used to promote or market any brand or product or service in that specific area to which the user belongs to. It also helps in enhancing the user profile.

In this blog, let us discuss about finding geographical location of a user using user’s IP address by just configuring Nginx with GeoIP MaxMind Databases and without doing any coding.

Nginx, an open source HTTP and IMAP/POP3 proxy server, is used as a main web server software or reverse proxy server for Apache. Its GeoIP module (ngx_http_geoip_module) uses precompiled MaxMind databases to set variables such as $geoip_country_name, $geoip_country_code, $geoip_city, and so on with values depending on client’s IP address.

Pre-requisites

  • Ubuntu Platform (Ubuntu 16.04, 12.04, 11.04)

Use Case

Install Nginx on Ubuntu, configure Nginx with GeoIP 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

Installing and Configuring Nginx on Ubuntu

To install and configure Nginx on Ubuntu, perform the following:

  • Install Nginx team’s package signing key using the following command:
  • Add the repo to your apt sources using the following commands:
  • Resynchronize and install the package index files using the following commands:

Installing GeoIP Module

GeoIP Module is used to lookup the IP address of a client machine connected with a server machine.

To install GeoIP module, perform the following steps:

  • Download and load the module to /usr/lib/nginx/modules using the following commands:
  • Open nginx.conf using the following command:
  • Add the below main context in the nginx.conf file:
Note: Skip the above steps if –with-http-geoip-module already exists on your version of Nginx.

To check the existence of the GeoIP module, use the below command:

Downloading GeoIP MaxMind GeoCity and GeoCountry Databases

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

Configuring Nginx with GeoIP MaxMind Databases

Nginx is configured with GeoIP 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 (NodeJS 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.

To get geolocation data of the user, use the following code written in Node.js:

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 Nginx on Ubuntu and configuring Nginx with GeoIP MaxMind GeoCity and GeoCountry Databases. We reverse proxied the sample web application through Nginx to find the geolocation of the user using IP address. In our upcoming blog, let us discuss about Nginx with GeoIP2 MaxMind Database to Fetch User Geolocation Data.

References

643 Views 2 Views Today