Malicious activities usually done via an anonymous proxy where the perpetrator’s identity was hidden from tracking. You can easily fall prey to the fraudsters unless with the right tools to detect a proxy. You need to a way to easily tell if a visitor is coming from a non-proxy traffic, an open proxy, VPN, TOR, a data center range or a residential ISP range.
In this article, we will guide you on how to install IP2Proxy 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 IP address from an anonymous proxy by using IP2Proxy Nginx module and IP2Proxy database.
Installation
This section teaches you on how to install the IP2Proxy 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 IP2Proxy C library.
wget https://github.com/ip2location/ip2proxy-c/archive/master.zip
- Decompress downloaded package and install it to your system.
unzip master.zip
cd ip2proxy-c-master
autoreconf -i -v --force
./configure
make
make install
cd ~/nginx-dev
- Download and decompress IP2Proxy Nginx module.
wget https://github.com/ip2location/ip2proxy-nginx/archive/master.zip
unzip master.zip
- Go to Nginx source codes and start compilation
cd ~/nginx-dev/nginx-VERSION
./configure --add-module=ip2proxy-nginx-master
make
make install
IP2Proxy Database Download
IP2Proxy offers 10 type of free LITE databases and commercial databases. The free database contains limited proxy information and updates monthly instead of daily in commercial edition.
- Create new directory for IP2Proxy database.
mkdir /etc/ip2proxy
cd /etc/ip2proxy - Go to https://lite.ip2location.com. Sign up an account for login and password.
- Download and decompress the latest IP2Proxy LITE database.
- wget “https://www.ip2location.com/download?login=YOUR_EMAIL_ADDRESS&password=YOUR_PASSWORD&productcode=PX11LITEBIN”
unzip IP2PROXY-LITE-PX11.BIN.ZIP
Configuration
You need to configure Nginx to use IP2Proxy module.
- Edit
/etc/nginx/nginx.conf
- Add following lines under `http` context:
http {
ip2proxy on;
ip2proxy_database /etc/ip2proxy/IP2PROXY-LITE-PX11.BIN;
ip2proxy_reverse_proxy on;
ip2proxy_access_type shared_memory;
}Syntax
Syntax:
ip2proxy on|off
Default: off
Context: http, server, location
Description: Enable or disable IP2Proxy Nginx module.Syntax:
ip2proxy_database path
Default: none
Context: http
Description: The absolute path to IP2Proxy BIN database.Syntax:
ip2proxy_access_type file_io|shared_memory|cache_memory
Default: shared_memory
Context: http
Description: Set the method used for lookup.Syntax:
ip2proxy_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:
ip2proxy_country_short
ip2proxy_country_long
ip2proxy_region
ip2proxy_city
ip2proxy_isp
ip2proxy_is_proxy
ip2proxy_proxy_type
ip2proxy_domain
ip2proxy_usage_type
ip2proxy_asn
ip2proxy_as
ip2proxy_last_seen
ip2proxy_threat
ip2proxy_provider
You may block the proxy traffic in Nginx as below:
if ( $ip2proxy_is_proxy = 'Y' ) { return 444; }