Get Started With IP2Location OpenResty Module

Dependencies

This library requires IP2Location BIN database to function. You may download the BIN database at


IPv4 BIN vs IPv6 BIN

Use the IPv4 BIN file if you just need to query IPv4 addresses.

Use the IPv6 BIN file if you need to query BOTH IPv4 and IPv6 addresses.


Installation

opm get ip2location/ip2location-resty

							
						

Sample Codes

Query geolocation information from BIN database

You can query the geolocation information from the IP2Location BIN database as below:

worker_processes  1;
error_log logs/error.log;
events {
    worker_connections 1024;
}
http {
    server {
        listen 8080 reuseport;
        location / {
            default_type text/html;
            content_by_lua_block {
                local ip2location = require('ip2location')
                local ip2loc = ip2location:new('/usr/local/ip2location/IP-COUNTRY-REGION-CITY-LATITUDE-LONGITUDE-ZIPCODE-TIMEZONE-ISP-DOMAIN-NETSPEED-AREACODE-WEATHER-MOBILE-ELEVATION-USAGETYPE-ADDRESSTYPE-CATEGORY-DISTRICT-ASN.BIN')
                local result = ip2loc:get_all('8.8.8.8')
                ngx.say("country_short: " .. result.country_short)
                ngx.say("country_long: " .. result.country_long)
                ngx.say("region: " .. result.region)
                ngx.say("city: " .. result.city)
                ngx.say("isp: " .. result.isp)
                ngx.say("latitude: " .. result.latitude)
                ngx.say("longitude: " .. result.longitude)
                ngx.say("domain: " .. result.domain)
                ngx.say("zipcode: " .. result.zipcode)
                ngx.say("timezone: " .. result.timezone)
                ngx.say("netspeed: " .. result.netspeed)
                ngx.say("iddcode: " .. result.iddcode)
                ngx.say("areacode: " .. result.areacode)
                ngx.say("weatherstationcode: " .. result.weatherstationcode)
                ngx.say("weatherstationname: " .. result.weatherstationname)
                ngx.say("mcc: " .. result.mcc)
                ngx.say("mnc: " .. result.mnc)
                ngx.say("mobilebrand: " .. result.mobilebrand)
                ngx.say("elevation: " .. result.elevation)
                ngx.say("usagetype: " .. result.usagetype)
                ngx.say("addresstype: " .. result.addresstype)
                ngx.say("category: " .. result.category)
                ngx.say("district: " .. result.district)
                ngx.say("asn: " .. result.asn)
                ngx.say("as: " .. result.as)
                ip2loc:close()
            }
        }
    }
}

							
						

IP2Location Sample Database (BIN)

Sample Packages - BIN File IPv4 IPv6
IP2Location DB1 Download (1,001.91 kB) Download (1.04 MB)
IP2Location DB2 Download (24.58 MB) Download (25.22 MB)
IP2Location DB3 Download (21.21 MB) Download (21.32 MB)
IP2Location DB4 Download (43.89 MB) Download (44.04 MB)
IP2Location DB5 Download (31.89 MB) Download (32.05 MB)
IP2Location DB6 Download (54.08 MB) Download (54.26 MB)
IP2Location DB7 Download (48.70 MB) Download (48.88 MB)
IP2Location DB8 Download (59.17 MB) Download (59.37 MB)
IP2Location DB9 Download (36.78 MB) Download (36.95 MB)
IP2Location DB10 Download (64.62 MB) Download (64.89 MB)
IP2Location DB11 Download (37.51 MB) Download (37.69 MB)
IP2Location DB12 Download (65.32 MB) Download (65.55 MB)
IP2Location DB13 Download (35.03 MB) Download (35.21 MB)
IP2Location DB14 Download (66.55 MB) Download (66.80 MB)
IP2Location DB15 Download (40.72 MB) Download (40.96 MB)
IP2Location DB16 Download (70.89 MB) Download (71.18 MB)
IP2Location DB17 Download (42.47 MB) Download (42.69 MB)
IP2Location DB18 Download (78.33 MB) Download (78.61 MB)
IP2Location DB19 Download (61.85 MB) Download (62.09 MB)
IP2Location DB20 Download (80.79 MB) Download (81.12 MB)
IP2Location DB21 Download (44.39 MB) Download (44.56 MB)
IP2Location DB22 Download (84.78 MB) Download (85.22 MB)
IP2Location DB23 Download (62.33 MB) Download (62.51 MB)
IP2Location DB24 Download (85.90 MB) Download (86.40 MB)
IP2Location DB25 Download (87.92 MB) Download (88.33 MB)
IP2Location DB26 Download (94.51 MB) Download (95.09 MB)