How to use IP2Location GeoLocation with Nginx

Geolocation is important, regardless of whether you are a marketer, online business owner, advertiser and so on. We need geolocation information to help us to learn more about our clients. For example, where are they coming from, what is their time zone, are they visiting via a proxy and many other reasons.

In this article, we will guide you on how to install IP2Location on a Debian platform and configure it to work with Nginx. At the end of this tutorial, you should be able to block or restrict users from a country from accessing your website by using IP2Location Nginx module and database.

Installation

This section teaches you on how to install the IP2Location Nginx module into Debian platform.

  1. Make sure you have installed all required packages for development.
    apt-get update && apt-get upgrade
    apt-get install build-essential dh-autoreconf unzip
  2. Create a working directory.
    mkdir ~/nginx-dev && cd ~/nginx-dev
  3. Download the latest Nginx source code from http://nginx.org/en/download.html
    wget http://nginx.org/download/nginx-VERSION.tar.gz
  4. Decompress the package.
    tar xvfz nginx-*.tar.gz
  5. Download latest IP2Location C library.
    wget https://github.com/chrislim2888/IP2Location-C-Library/archive/master.zip
  6. Decompress downloaded package and install it to your system.
    unzip master.zip
    cd IP2Location-C-Library-master
    autoreconf -i -v --force
    ./configure
    make
    make install
    cd ~/nginx-dev
  7. Download and decompress IP2Location Nginx module.
    wget https://github.com/ip2location/ip2location-nginx/archive/master.zip
    unzip master.zip
  8. Go to Nginx source codes and start compilation.
    cd ~/nginx-dev/nginx-VERSION
    ./configure --add-module=ip2location-nginx-master
    make
    make install

IP2Location Database Download

IP2Location offers 5 free LITE databases and 24 commercial IP geolocation databases. Free database is less accurate comparing to commercial database.

  1. Create new directory for IP2Location database.
    mkdir /etc/ip2location
    cd /etc/ip2location
  2. Go to https://lite.ip2location.com. Sign up an account for login and password.
  3. Download and decompress the latest IP2Location LITE database.

  4. http://download.ip2location.com/lite/IP2LOCATION-LITE-DB1.BIN.ZIP
    unzip IP2LOCATION-LITE-DB1.BIN.ZIP

Configuration

You need to configure Nginx to use IP2LOCATION module.

  1. Edit

    /etc/nginx/nginx.conf
  2. Add following lines under `http` context:

    http {
    ip2location on;
    ip2location_database /root/bin/DB1.BIN;
    ip2location_reverse_proxy on;
    ip2location_access_type shared_memory;
    }

    Syntax

    Syntax: ip2location on|off
    Default: off
    Context: http, server, location
    Description: Enable or disable IP2LOCATION Nginx module.

    Syntax: ip2location_database path
    Default: none
    Context: http
    Description: The absolute path to IP2LOCATION BIN database.

    Syntax: ip2location_access_type file_io|shared_memory|cache_memory
    Default: shared_memory
    Context: http
    Description: Set the method used for lookup.

    Syntax: ip2location_proxy cidr|address
    Default: none
    Context: http
    Description: Set a list of proxies to translate x-forwarded-for headers for.

    Syntax: ip2location_proxy_recursive on|off
    Default: off
    Context: http
    Description: Enable recursive search in the x-forwarded-for headers.

    Variables

    The following variables will be made available in Nginx: ip2location_country_shortip2location_country_longip2location_regionip2location_cityip2location_ispip2location_latitudeip2location_longitudeip2location_domainip2location_zipcodeip2location_timezoneip2location_netspeedip2location_iddcodeip2location_areacodeip2location_weatherstationcodeip2location_weatherstationnameip2location_mccip2location_mncip2location_elevationip2location_usagetype

    You may block the proxy traffic in Nginx as below:

    if ( $ip2location_country_short == 'US' ) {
        return 444;
    }

     

Was this article helpful?

Related Articles