{"id":91368,"date":"2020-05-01T13:43:35","date_gmt":"2020-05-01T05:43:35","guid":{"rendered":"http:\/\/4563.org\/?p=91368"},"modified":"2020-05-01T13:43:35","modified_gmt":"2020-05-01T05:43:35","slug":"ip2location-nginx-module-%e9%85%8d%e7%bd%ae%e4%bd%bf%e7%94%a8%e5%b0%8f%e7%bb%93","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=91368","title":{"rendered":"IP2Location Nginx Module \u914d\u7f6e\u4f7f\u7528\u5c0f\u7ed3"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  IP2Location Nginx Module \u914d\u7f6e\u4f7f\u7528\u5c0f\u7ed3               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : wsgzao <\/span>  <span><i><\/i> 22<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<h2>\u524d\u8a00<\/h2>\n<p>IP2Location \u4e3b\u8981\u662f\u7528\u4e8e\u4ee3\u66ff MaxMind GeoIP\uff0c\u539f\u56e0\u662f GeoIP \u6570\u636e\u5e93\u9488\u5bf9\u4e2d\u56fd\u7684 Blacklist \u9ed1\u540d\u5355\u6709\u975e\u5e38\u9ad8\u7684\u8bef\u4f24\u7387\uff0c\u9009\u62e9 IP2Location \u53ef\u4ee5\u6709\u6548\u964d\u4f4e\u8bef\u4f24\uff0c\u4e3a\u4e86\u4e1a\u52a1\u9700\u6c42\u5f97\u53ca\u65f6\u505a\u51fa\u6539\u53d8\u3002\u5728\u4f7f\u7528 IP2Location \u7684\u8fc7\u7a0b\u4e2d\u53d1\u73b0\u5b98\u7f51\u7684\u6b65\u9aa4\u8fd8\u662f\u5b58\u5728\u4e00\u4e9b\u95ee\u9898\uff0c\u8fd9\u91cc\u8bb0\u5f55\u548c\u5206\u4eab\u4e0b\u81ea\u5df1\u9010\u6b65\u89e3\u51b3\u95ee\u9898\u7684\u8fc7\u7a0b\u3002<\/p>\n<h2>\u66f4\u65b0\u5386\u53f2<\/h2>\n<p>2020 \u5e74 04 \u6708 21 \u65e5 &#8211; \u521d\u7a3f<\/p>\n<p>\u9605\u8bfb\u539f\u6587 &#8211; https:\/\/wsgzao.github.io\/post\/ip2location\/<\/p>\n<h2>GeoIP \u548c IP2Location \u7b80\u4ecb<\/h2>\n<p>GeoIP\u662f\u4e00\u5957\u542b IP \u6570\u636e\u5e93\u7684\u8f6f\u4ef6\u5de5\u5177\u3002\u9664\u6b64\u4e4b\u5916\u8fd8\u6709IP2Location\u7b49\uff0c\u56fd\u5185\u505a\u5f97\u6bd4\u8f83\u6df1\u5165\u7684\u662f\u9ad8\u6625\u8f89\u521b\u5efa\u7684IPIP.NET<\/p>\n<p>Geo \u6839\u636e\u6765\u8bbf\u8005\u7684 IP\uff0c \u5b9a\u4f4d\u8be5 IP \u6240\u5728\u7ecf\u7eac\u5ea6\u3001\u56fd\u5bb6 \/\u5730\u533a\u3001\u7701\u5e02\u3001\u548c\u8857\u9053\u7b49\u4f4d\u7f6e\u4fe1\u606f\u3002<\/p>\n<p>GeoIP\/IP2Location \u7b49\u901a\u5e38\u6709\u4e24\u4e2a\u7248\u672c\uff0c\u4e00\u4e2a\u514d\u8d39\u7248\uff0c\u4e00\u4e2a\u6536\u8d39\u7248\u672c\u3002<\/p>\n<p>\u6536\u8d39\u7248\u672c\u7684\u51c6\u786e\u7387\u9ad8\u4e00\u4e9b\uff0c\u66f4\u65b0\u9891\u7387\u4e5f\u66f4\u9891\u7e41\u3002<\/p>\n<p>Geo IP solution to identify country, region, city, latitude &amp; 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.<\/p>\n<p>GeoIP \u662f\u5927\u5bb6\u90fd\u975e\u5e38\u719f\u6089\u7684\u8001\u5b57\u53f7\uff0c\u800c\u8fd9\u6b21\u7684\u4e3b\u89d2\u662f IP2Location<\/p>\n<h2>IP2Location Nginx Module<\/h2>\n<p>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.<\/p>\n<p>https:\/\/www.ip2location.com\/development-libraries\/ip2location\/nginx<\/p>\n<h3>Installation<\/h3>\n<p>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\u2122 binary databases for IPv4 and IPv6 address.<\/p>\n<ul>\n<li>\n<p>Download IP2location C library from here.<\/p>\n<\/li>\n<li>\n<p>Download and decompress this Nginx module package.<\/p>\n<\/li>\n<li>\n<p>Change the path to IP2Location library in &#8220;ngx_http_ip2location_module.c&#8221;.<\/p>\n<\/li>\n<li>\n<p>Re-compile Nginx from source to include this module. Add the below directive into the compile of Nginx:<\/p>\n<p>.\/configure &#8211;add-module=\/absolute\/path\/to\/nginx-ip2location-8.0.0 make make install<\/p>\n<\/li>\n<li>\n<p>Edit your Nginx config file to point the correct path of IP2Location database file:<\/p>\n<p>ip2location_database \/absolute\/path\/to\/IP2LOCATION-DB1.BIN;<\/p>\n<\/li>\n<\/ul>\n<h3>\u5b89\u88c5\u5907\u6ce8<\/h3>\n<p>IP2Location \u5b98\u65b9\u7684\u6267\u884c\u6b65\u9aa4\u5b58\u5728\u4e00\u4e9b\u95ee\u9898\u6ca1\u6709\u8bf4\u6e05\u695a\uff0c\u8fd9\u91cc\u5206\u4eab\u81ea\u5df1\u5b9e\u8df5\u540e\u7684\u6b65\u9aa4\u548c\u7ed3\u8bba<\/p>\n<blockquote>\n<p>\u5b89\u88c5\u4f9d\u8d56\u5305<\/p>\n<\/blockquote>\n<pre><code># 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 <\/code><\/pre>\n<blockquote>\n<p>\u7f16\u8bd1 IP2Location C library<\/p>\n<\/blockquote>\n<pre><code>git clone https:\/\/github.com\/chrislim2888\/IP2Location-C-Library cd IP2Location-C-Library autoreconf -i -v --force .\/configure make make install # \u4ee5\u4e0b\u6b65\u9aa4\u53ef\u9009 cd data perl ip-country.pl cd ..\/test .\/test-IP2Location <\/code><\/pre>\n<blockquote>\n<p>\u7f16\u8bd1 Nginx<\/p>\n<\/blockquote>\n<p>nginx: download<\/p>\n<pre><code># 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 # \u4e00\u822c\u7f16\u8bd1 nginx \u4e8c\u8fdb\u5236\u6587\u4ef6\u4e0d\u4f1a\u51fa\u73b0\u8be5\u95ee\u9898\uff0c\u5982\u679c\u4f60\u4f7f\u7528 rpmbuild \u6253\u5305\u5c31\u8981\u6ce8\u610f\u4e86 rpm -Uvh https:\/\/rpms.remirepo.net\/enterprise\/7\/remi\/x86_64\/libip2location-8.0.7-1.el7.remi.x86_64.rpm  <\/code><\/pre>\n<h2>IP2Location Database Download<\/h2>\n<p>IP2Location offers 5 free LITE databases and 24 commercial IP geolocation databases. Free database is less accurate comparing to commercial database.<\/p>\n<pre><code># 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 <\/code><\/pre>\n<h2>Configuration<\/h2>\n<p>You need to configure Nginx to use IP2LOCATION module.<\/p>\n<pre><code># 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; }  <\/code><\/pre>\n<p>\u53ef\u9009\u53c2\u6570 ip2location_access_type file_io|shared_memory|cache_memory \u9ed8\u8ba4\u4e3a shared_memory \u5efa\u8bae\u4e0d\u8981\u9009\u62e9 file_io\uff0c \u5426\u5219\u53ef\u80fd\u4f1a\u4e25\u91cd\u62d6\u6162\u54cd\u5e94\u901f\u5ea6<\/p>\n<h2>Syntax<\/h2>\n<p>Syntax:\u00a0<code>ip2location on|off<\/code> Default: off Context: http, server, location Description: Enable or disable IP2LOCATION Nginx module.<\/p>\n<p>Syntax:\u00a0<code>ip2location_database path<\/code> Default: none Context: http Description: The absolute path to IP2LOCATION BIN database.<\/p>\n<p>Syntax:\u00a0<code>ip2location_access_type file_io|shared_memory|cache_memory<\/code> Default: shared_memory Context: http Description: Set the method used for lookup.<\/p>\n<p>Syntax:\u00a0<code>ip2location_proxy cidr|address<\/code> Default: none Context: http Description: Set a list of proxies to translate x-forwarded-for headers for.<\/p>\n<p>Syntax:\u00a0<code>ip2location_proxy_recursive on|off<\/code> Default: off Context: http Description: Enable recursive search in the x-forwarded-for headers.<\/p>\n<h2>Variables<\/h2>\n<p>The following variables will be made available in Nginx:<\/p>\n<pre><code>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 <\/code><\/pre>\n<p>You may block the traffic from United States in Nginx as below:<\/p>\n<pre><code>if ( $ip2location_country_short = 'US' ) {     return 444; }  if ( $ip2location_country_short = 'SG' ) {     return 444; } <\/code><\/pre>\n<blockquote>\n<p>\u8fd8\u53ef\u4ee5\u53c2\u7167 GeoIP \u7684\u914d\u7f6e\u65b9\u6cd5<\/p>\n<\/blockquote>\n<pre><code>map $ip2location_country_short $blacklist_country {     default no;     CN yes; }  server {         listen 80;         server_name wangao.com;         if ($blacklist_country = yes) {             return 444;         } } <\/code><\/pre>\n<blockquote>\n<p>\u6d4f\u89c8\u5668\u8bbf\u95ee\u68c0\u67e5 nginx log \u7ed3\u679c<\/p>\n<\/blockquote>\n<pre><code>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\" <\/code><\/pre>\n<h2>IP2Location Python Library<\/h2>\n<p>This module is a Python Library to support all IP2Location\u2122 database products. It has been optimized for speed and memory utilization. Developers can use this API to query all IP2Location\u2122 binary databases for IPv4 and IPv6 address.<\/p>\n<p>https:\/\/www.ip2location.com\/development-libraries\/ip2location\/python<\/p>\n<pre><code>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 <\/code><\/pre>\n<pre><code>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)  <\/code><\/pre>\n<h2>\u53c2\u8003\u6587\u7ae0<\/h2>\n<p>How to use IP2Location GeoLocation with Nginx<\/p>\n<p>How to Install IP2Location Nginx Module on Debian<\/p>\n<p>Upgrade to GeoIP2 with NGINX on CentOS\/RHEL<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>3<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"1246163\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : royan <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4e0d\u9519\uff0c\u652f\u6301\uff01                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1246164\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : aveline <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5982\u679c\u53ea\u662f\u8fc7\u6ee4\u4e2d\u56fd IP\uff0c\u53ef\u4ee5\u8bd5\u8bd5\u6211\u4eec\u7684 chnroutes2\uff0c\u6bcf\u5c0f\u65f6\u5b9a\u65f6\u4ece\u8def\u7531\u4e0a\u6293\u7684\u6570\u636e\uff1a<br \/>https:\/\/github.com\/misakaio\/chnroutes2<\/p>\n<p>IP2Location \u7684\u51c6\u786e\u5ea6\u975e\u5e38\u5dee\uff0c\u800c\u4e14\u7ecf\u5e38\u778e\u731c\u5730\u5740\uff0c\u6bd4\u5982\u8bf4 WHOIS \u91cc\u6211\u4eec\u7684\u4ee5\u524d\u6ce8\u518c\u5730\u5740\u662f\u7f8e\u56fd DE \u7684 New Castle\uff0c\u4ed6\u4eec\u4f1a\u6807\u6210 OK \u7684 Castle\uff0c\u6211\u4eec\u4fc4\u7f57\u65af\u7684 IP \u4e5f\u7ecf\u5e38\u4f1a\u88ab\u6807\u5728 LA \u3002\u6c9f\u901a\u5f88\u7d2f\uff0c\u5982\u679c\u4e0d\u662f\u8fd9\u4e2a\u7528\u9014\u4e2a\u4eba\u8fd8\u662f\u5efa\u8bae\u7ee7\u7eed\u7528 GeoIP \u6216\u8005 ipinfo \u3002IPIP \u76ee\u524d\u7684\u66f4\u65b0\u901f\u5ea6\u662f\u5168\u7403\u6700\u5feb\u7684\uff0c\u51c6\u786e\u7387\u4e5f\u4e0d\u9519\uff0c\u53ea\u662f\u4ef7\u683c\u5f88\u8d35\u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1246165\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : wsgzao <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @aveline #2 <br \/>\u8c22\u8c22\u8fd9\u4e48\u8be6\u7ec6\u7684\u4e13\u4e1a\u56de\u590d\uff0cGeoIP \u548c IP2Location \u5546\u4e1a\u7248\u90fd\u91c7\u8d2d\u4e86\u4f46\u53d1\u73b0\u540e\u8005\u8bef\u4f24\u4f4e\uff0c\u4e4b\u524d\u4e00\u76f4\u4f7f\u7528 GeoIP\uff0c\u56e0\u4e3a\u539f\u672c\u7684\u4f5c\u7528\u5176\u5b9e\u5e76\u4e0d\u662f\u8fc7\u6ee4\u4e2d\u56fd\uff0c\u800c\u662f\u8981\u505a\u6e38\u620f\u8fd0\u8425\u4e0a\u7684\u7ec6\u5206\uff0c\u8de8\u56fd\u505a\u4e00\u4e9b\u9ed1\u767d\u540d\u5355\u673a\u5236\u3002\u56e0\u4e3a\u7b2c\u4e00\u6b21\u4f7f\u7528 IP2Location\uff0c\u53d1\u73b0\u5b98\u7f51\u548c Google \u641c\u7d22\u51fa\u6765\u7684\u6587\u7ae0\u5728\u914d\u7f6e\u4ecb\u7ecd\u4e0a\u90fd\u6bd4\u8f83\u6a21\u7cca\uff0c\u4e8e\u662f\u5c31\u5206\u4eab\u4e86\u8fd9\u7bc7\u7b80\u5355\u7684\u6587\u7ae0\u3002\u4e0d\u8fc7\u8fd8\u662f\u975e\u5e38\u611f\u8c22\u4f60\u7684\u56de\u590d\uff0c\u7ed9\u6211\u4eec\u63d0\u4f9b\u4e86\u65b0\u7684\u89e3\u51b3\u601d\u8def\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>IP2Location Nginx&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/91368"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=91368"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/91368\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=91368"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=91368"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=91368"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}