Geo IP solution to identify country, region, city, latitude & longitude, ZIP code, time zone, connection speed, ISP, domain name, IDD country code, area code, weather station data, mobile network codes (MNC), mobile country codes (MCC), mobile carrier, elevation and usage type.
GeoIP 是大家都非常熟悉的老字号,而这次的主角是 IP2Location
IP2Location Nginx Module
This is an IP2Location Nginx Module that enables the user to identify the country code and country name by IP address. In general, it is faster, easier and more accurate than reverse DNS lookups.
IP2Location C library enables the user to find the country, region, city, coordinates, ZIP code, time zone, ISP, domain name, connection type, area code, weather, MCC, MNC, mobile brand name, elevation and usage type that any IP address or hostname originates from. It has been optimized for speed and memory utilization. Developers can use the API to query all IP2Location™ binary databases for IPv4 and IPv6 address.
# These are for RedHat, CentOS, and Fedora. sudo yum install wget git gcc-c++ pcre-devel zlib-devel make libtool autoconf automake
# These are for Debian. Ubuntu will be similar. sudo apt-get install wget git build-essential zlib1g-dev libpcre3 libpcre3-dev libtool autoconf automake
编译 IP2Location C library
1 2 3 4 5 6 7 8 9 10 11
git clone https://github.com/chrislim2888/IP2Location-C-Library cd IP2Location-C-Library autoreconf -i -v --force ./configure make make install # 以下步骤可选 cd data perl ip-country.pl cd ../test ./test-IP2Location
# IP2Location library in "ngx_http_ip2location_module.c" cd ip2location-nginx vim ngx_http_ip2location_module.c #include "IP2Location.h" #include "/root/ip2location/IP2Location-C-Library-master/libIP2Location/IP2Location.h"
# Download Nginx Stable version VERSION="1.16.1" wget http://nginx.org/download/nginx-${VERSION}.tar.gz tar -xvzf nginx-${VERSION}.tar.gz cd nginx-${VERSION}
# Compile Nginx ./configure --add-module=../ip2location-nginx make sudo make install
# error: Failed dependencies: # libIP2Location.so.1()(64bit) is needed by nginx-garena-1.16.1-0.noarch # 一般编译 nginx 二进制文件不会出现该问题,如果你使用 rpmbuild 打包就要注意了 rpm -Uvh https://rpms.remirepo.net/enterprise/7/remi/x86_64/libip2location-8.0.7-1.el7.remi.x86_64.rpm
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 2 3 4 5 6 7 8
# Create new directory for IP2Location database. mkdir -p /usr/share/ip2location cd /usr/share/ip2location
# Go to https://lite.ip2location.com. Sign up an account for login and password. # Download and decompress the latest IP2Location LITE database. wget 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.
server { listen 80; server_name wangao.com; if ($blacklist_country = yes) { return 444; } }
浏览器访问检查 nginx log 结果
1 2 3 4 5
tailf /var/log/nginx/access.log
xxx - - [21/Apr/2020:17:18:11 +0800] "GET / HTTP/1.1" 200 396 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36"
xxx - - [21/Apr/2020:17:18:42 +0800] "GET / HTTP/1.1" 444 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36"
IP2Location Python Library
This module is a Python Library to support all IP2Location™ database products. It has been optimized for speed and memory utilization. Developers can use this API to query all IP2Location™ binary databases for IPv4 and IPv6 address.
import IP2Location IP2LocObj = IP2Location.IP2Location() ''' Cache the database into memory to accelerate lookup speed. WARNING: Please make sure your system have sufficient RAM to use this feature. ''' # database = IP2Location.IP2Location(os.path.join("data", "IPV6-COUNTRY.BIN"), "SHARED_MEMORY") IP2LocObj.open("data/IP-COUNTRY-REGION-CITY-LATITUDE-LONGITUDE-ZIPCODE-TIMEZONE-ISP-DOMAIN-NETSPEED-AREACODE-WEATHER-MOBILE-ELEVATION-USAGETYPE-SAMPLE.BIN") rec = IP2LocObj.get_all("19.5.10.1") print rec.country_short
defip2location_search(ip, db): IP2LocObj = IP2Location.IP2Location() ''' Cache the database into memory to accelerate lookup speed. WARNING: Please make sure your system have sufficient RAM to use this feature. ''' # database = IP2Location.IP2Location(os.path.join("data", "IPV6-COUNTRY.BIN"), "SHARED_MEMORY") IP2LocObj.open(db) rec = IP2LocObj.get_all(ip) print rec.country_short
def_parse_args(): parser = argparse.ArgumentParser(description="Search IP in IP2Location Database") parser.add_argument("-i", "--ip", help="Input ip", required=True) parser.add_argument("-d", "--db", help="Path to ip2location db", required=True) return parser.parse_args()
if __name__ == "__main__": args = _parse_args() ip = args.ip db = args.db ip2location_search(ip, db)
Country blocking using Nginx with IP2Location Module
After few days of trying, trying and trying, finally find out the way to compile the IP2Location module into the Nginx in Ubuntu server. I try to compare multiple way of doing the country block with Nginx, where in my previous post, the country blocking is done with GeoIP module (which I found out more easy compare to the IP2Location module), and also the country block without any 3rd party plugin for Nginx, with this method, that’s no any compilation of Nginx needed in order to make it, it’s most suitable for the current running Nginx which install directly from the APT repo.
IP2Location is one of the most famous IP location provider in the market and they are from Penang. I being use their service since many years ago and that’s the reason why I wanna try out to doing the Nginx country block with IP2Location module.
I can’t really find a full and complete step on the installing and compiling of the module, that’s why it take me many days to figure out how to make the whole thing work nicely.
The following script basically just download all necessary module source code (OpenSSL, PCRE, IP2Location C module, IP2Lacation Nginx module, Nginx 1.15 .0 source code) and compile all. After that, create the system service and put the Nginx into auto start mode and include the IP2Location database into Nginx.conf. Beside that, I also include the script to auto download the database directly from IP2Location server.
sudo sed -i 's/default_type application\/octet-stream;/default_type application\/octet-stream;\nip2location on;\nip2location_database \/etc\/ip2location\/IP-COUNTRY.BIN;\nip2location_access_type shared_memory;/g' /etc/nginx/nginx.conf
After finish run above command, just add the following script into your Nginx.conf or your website virtual host directive in or to make rewrite all incoming traffic to your desire destination
Finally you may testing your configure by browsing the server IP using any of your browse, if you are from Malaysia, than you should be redirected to Google home page