IP2Location主要是用於代替MaxMind GeoIP,緣由是GeoIP數據庫針對中國的Blacklist黑名單有很是高的誤傷率,選擇IP2Location能夠有效下降誤傷,爲了業務需求得及時作出改變。在使用IP2Location的過程當中發現官網的步驟仍是存在一些問題,這裏記錄和分享下本身逐步解決問題的過程。html
2020年04月21日 - 初稿python
閱讀原文 - wsgzao.github.io/post/ip2loc…nginx
GeoIP是一套含IP數據庫的軟件工具。除此以外還有IP2Location等,國內作得比較深刻的是高春輝建立的IPIP.NETc++
Geo根據來訪者的IP, 定位該IP所在經緯度、國家/地區、省市、和街道等位置信息。git
GeoIP/IP2Location等一般有兩個版本,一個免費版,一個收費版本。github
收費版本的準確率高一些,更新頻率也更頻繁。數據庫
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.vim
GeoIP是你們都很是熟悉的老字號,而此次的主角是IP2Locationcentos
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.瀏覽器
www.ip2location.com/development…
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.
Download IP2location C library from here.
Download and decompress this Nginx module package.
Change the path to IP2Location library in "ngx_http_ip2location_module.c".
Re-compile Nginx from source to include this module. Add the below directive into the compile of Nginx:
./configure --add-module=/absolute/path/to/nginx-ip2location-8.0.0 make make install
Edit your Nginx config file to point the correct path of IP2Location database file:
ip2location_database /absolute/path/to/IP2LOCATION-DB1.BIN;
IP2Location官方的執行步驟存在一些問題沒有說清楚,這裏分享本身實踐後的步驟和結論
安裝依賴包
# 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
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
複製代碼
編譯 Nginx
# Download ip2location-nginx
git clone https://github.com/ip2location/ip2location-nginx
# 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 offers 5 free LITE databases and 24 commercial IP geolocation databases. Free database is less accurate comparing to commercial database.
# 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
複製代碼
You need to configure Nginx to use IP2LOCATION module.
# Edit
vi /etc/nginx/nginx.conf
# Add following lines under `http` context:
http {
ip2location on;
ip2location_database /usr/share/ip2location/IP2LOCATION-LITE-DB1.BIN;
#ip2location_database /usr/share/ip2location/IP-COUNTRY-REGION-CITY-LATITUDE-LONGITUDE-ZIPCODE-ISP-DOMAIN.BIN
ip2location_access_type shared_memory;
}
複製代碼
可選參數 ip2location_access_type file_io|shared_memory|cache_memory 默認爲 shared_memory 建議不要選擇 file_io, 不然可能會嚴重拖慢響應速度
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.
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
複製代碼
You may block the traffic from United States in Nginx as below:
if ( $ip2location_country_short = 'US' ) {
return 444;
}
if ( $ip2location_country_short = 'SG' ) {
return 444;
}
複製代碼
還能夠參照GeoIP的配置方法
map $ip2location_country_short $blacklist_country {
default no;
CN yes;
}
server {
listen 80;
server_name wangao.com;
if ($blacklist_country = yes) {
return 444;
}
}
複製代碼
瀏覽器訪問檢查nginx log結果
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"
複製代碼
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.
www.ip2location.com/development…
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
複製代碼
import IP2Location
import sys
import argparse
def ip2location_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)
複製代碼
How to use IP2Location GeoLocation with Nginx