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.


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
  4. Decompress the package.
    tar xvfz nginx-*.tar.gz
  5. Download latest IP2Location C library.
  6. Decompress downloaded package and install it to your system.
    cd IP2Location-C-Library-master
    autoreconf -i -v --force
    make install
    cd ~/nginx-dev
  7. Download and decompress IP2Location Nginx module.
  8. Go to Nginx source codes and start compilation.
    cd ~/nginx-dev/nginx-VERSION
    ./configure --add-module=ip2location-nginx-master
    make install

IP2Location Database Download

IP2Location offers 5 free LITE databases and 25 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 Sign up an account for login and password.
  3. Download and decompress the latest IP2Location LITE database.



You need to configure Nginx to use IP2LOCATION module.

  1. Edit

  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: 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.


    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_usagetypeip2location_addresstypeip2location_category

    You may block the traffic from United States in Nginx as below:

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


Was this article helpful?

Related Articles