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.
- Make sure you have installed all required packages for development.
apt-get update && apt-get upgrade apt-get install build-essential dh-autoreconf unzip
- Create a working directory.
mkdir ~/nginx-dev && cd ~/nginx-dev
- Download the latest Nginx source code from http://nginx.org/en/download.html
wget http://nginx.org/download/nginx-VERSION.tar.gz
- Decompress the package.
tar xvfz nginx-*.tar.gz
- Download latest IP2Location C library.
wget https://github.com/chrislim2888/IP2Location-C-Library/archive/master.zip
- 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
- Download and decompress IP2Location Nginx module.
wget https://github.com/ip2location/ip2location-nginx/archive/master.zip
unzip master.zip
- 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 25 commercial IP geolocation databases. Free database is less accurate comparing to commercial database.
- Create new directory for IP2Location database.
mkdir /etc/ip2location
cd /etc/ip2location
- Go to https://lite.ip2location.com. Sign up an account for login and password.
- Download and decompress the latest IP2Location LITE database.
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.
- Edit
/etc/nginx/nginx.conf
- 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_short
ip2location_country_long
ip2location_region
ip2location_city
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
ip2location_addresstype
ip2location_category
You may block the traffic from United States in Nginx as below:
if ( $ip2location_country_short = 'US' ) { return 444; }