How to use IP2Proxy with Nginx

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 or a data center 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.

  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 IP2Proxy C library.
    wget https://github.com/ip2location/ip2proxy-c/archive/master.zip
  6. 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
  7. Download and decompress IP2Proxy Nginx module.

    wget https://github.com/ip2location/ip2proxy-nginx/archive/master.zip
    unzip master.zip
  8. 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 4 type of free LITE databases and commercial databases. The free database contains limited proxy information and updates monthly instead of daily in commercial edition.

  1. Create new directory for IP2Proxy database.
    mkdir /etc/ip2proxy
    cd /etc/ip2proxy
  2. Go to https://lite.ip2location.com. Sign up an account for login and password.
  3. Download and decompress the latest IP2Proxy LITE database.
  4. wget “https://www.ip2location.com/download?login=YOUR_EMAIL_ADDRESS&password=YOUR_PASSWORD&productcode=PX4LITEBIN”
    unzip IP2PROXY-LITE-PX4.BIN.ZIP

Configuration

You need to configure Nginx to use IP2Proxy module.

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

    http {
    ip2proxy on;
    ip2proxy_database /etc/ip2proxy/IP2PROXY-LITE-PX4.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_shortip2proxy_country_longip2proxy_regionip2proxy_cityip2proxy_ispip2proxy_is_proxyip2proxy_proxy_type

    You may block the proxy traffic in Nginx as below:

    if ( $ip2proxy_is_proxy == 'Y' ) {
        return 444;
    }

     

Was this article helpful?

Related Articles