ip2location facebbook  ip2location twitter  ip2location google+ ip2location github

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.
  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 IP2Proxy C library.
  10. 						   
    						   wget https://github.com/ip2location/ip2proxy-c/archive/master.zip
    						   					
    						
  11. Decompress downloaded package and install it to your system.
  12. 						   
    						   unzip master.zip
    						   cd ip2proxy-c-master
    						   autoreconf -i -v --force
    						   ./configure
    						   make
    						   make install
    						   cd ~/nginx-dev
    						   					
    						
  13. Download and decompress IP2Proxy Nginx module.
  14. 						   
    						    wget https://github.com/ip2location/ip2proxy-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=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.
  2. 							
    							mkdir /etc/ip2proxy
    							cd /etc/ip2proxy
    							
    						
  3. Go to http://lite.ip2location.com. Sign up an account for login and password.
  4. Download and decompress the latest IP2Proxy LITE database.
  5. 							
    							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:
  3. 							   
    								   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_short
ip2proxy_country_long
ip2proxy_region
ip2proxy_city
ip2proxy_isp
ip2proxy_is_proxy
ip2proxy_proxy_type

You may block the proxy traffic in Nginx as below:

						
						if ( $ip2proxy_is_proxy == 'Y' ) {
							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.