ip2location facebbook  ip2location twitter  ip2location google+ ip2location github

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.
  2. 
    						   apt-get update && apt-get upgrade
    						   apt-get install build-essential dh-autoreconf unzip
    
    						
  3. Create a working directory.
  4. 
    						   mkdir ~/nginx-dev && cd ~/nginx-dev
    
    						 
  5. Download the latest Nginx source code from http://nginx.org/en/download.html
  6. 
    						   wget http://nginx.org/download/nginx-VERSION.tar.gz
    
    						 
  7. Decompress the package.
  8. 
    						   tar xvfz nginx-*.tar.gz
    
    						
  9. Download latest IP2Location C library.
  10. 
    						   wget https://github.com/chrislim2888/IP2Location-C-Library/archive/master.zip
    
    						
  11. Decompress downloaded package and install it to your system.
  12. 
    						   unzip master.zip
    						   cd IP2Location-C-Library-master
    						   autoreconf -i -v --force
    						   ./configure
    						   make
    						   make install
    						   cd ~/nginx-dev
    
    						
  13. Download and decompress IP2Location Nginx module.
  14. 
    						    wget https://github.com/ip2location/ip2location-nginx/archive/master.zip
    							unzip master.zip
    
    						
  15. Go to Nginx source codes and start compilation.
  16. 
    						    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.
  2. 
    							mkdir /etc/ip2location
    							cd /etc/ip2location
    
    						
  3. Go to http://lite.ip2location.com. Sign up an account for login and password.
  4. Download and decompress the latest IP2Location LITE database.
  5. 
    							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:
  3. 
    								   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_short
ip2location_country_long
ip2location_region
ip2location_citybr /> ip2location_isp
ip2location_latitude
ip2location_longitude
ip2location_domain
ip2location_zipcode
ip2location_timezone
ip2location_netspeed
ip2location_iddcode
ip2location_areacode
ip2location_weatherstationcode
ip2location_weatherstationname
ip2location_mcc
ip2location_mnc
ip2location_elevation
ip2location_usagetype

You may block the proxy traffic in Nginx as below:


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

					


Do you like this article? Share it with others by clicking the social media buttons below. We will write more articles related to this topic.