### Run OpenResty on Ubuntu with MaxMind DB Source: https://github.com/anjia0532/lua-resty-maxminddb/blob/master/docker/README.md This section provides instructions for running OpenResty on an Ubuntu Docker image. It covers starting OpenResty as a service, mounting the Lua library and GeoIP data, and configuring Nginx. It also includes steps to install `libmaxminddb` on the Ubuntu image using `apt` and a PPA. ```bash # run openresty as daemon docker run -d -p80:80 --name openresty --rm -v $(pwd)/../lib/resty/maxminddb.lua:/usr/local/openresty/lualib/resty/maxminddb.lua -v $(pwd)/geoipupdate_data/:/opt/geoipupdate_data/ -v $(pwd)/nginx.conf:/usr/local/openresty/nginx/conf/nginx.conf openresty/openresty:jammy # install libmaxminddb docker exec -it openresty /bin/bash apt update && apt install -y software-properties-common && add-apt-repository ppa:maxmind/ppa -y && apt update && apt install -y libmaxminddb0 libmaxminddb-dev mmdb-bin openresty -s reload ``` -------------------------------- ### Run OpenResty on Alpine with MaxMind DB Source: https://github.com/anjia0532/lua-resty-maxminddb/blob/master/docker/README.md This section details running OpenResty on an Alpine Linux Docker image. It includes instructions for starting OpenResty as a daemon, mounting the `resty/maxminddb.lua` library and the downloaded GeoIP data, and configuring Nginx. It also covers installing `libmaxminddb` on the Alpine image, with specific steps for versions 1.9.1-r0 and 1.12.2. ```bash # run openresty as daemon docker run -d -p80:80 --name openresty --rm -v $(pwd)/../lib/resty/maxminddb.lua:/usr/local/openresty/lualib/resty/maxminddb.lua -v $(pwd)/geoipupdate_data/:/opt/geoipupdate_data/ -v $(pwd)/nginx.conf:/usr/local/openresty/nginx/conf/nginx.conf openresty/openresty:alpine-fat # install libmaxminddb docker exec -it openresty /bin/bash # 1.9.1-r0 apkg add --no-cache perl libmaxminddb && ln -s /usr/lib/libmaxminddb.so.0 /usr/lib/libmaxminddb.so # 1.12.2 cd /tmp/ wget https://github.com/maxmind/libmaxminddb/releases/download/1.12.2/libmaxminddb-1.12.2.tar.gz && \ tar zxf libmaxminddb-1.12.2.tar.gz && \ cd libmaxminddb-1.12.2 && \ ./configure && \ make -j && \ make check && \ make install && \ ldconfig && \ rm -rf /tmp/* openresty -s reload ``` -------------------------------- ### Install lua-resty-maxminddb via OPM Source: https://github.com/anjia0532/lua-resty-maxminddb/blob/master/README.md Installs the lua-resty-maxminddb library using the OPM package manager. Requires manual installation of libmaxminddb and downloading a GeoLite2 City database. ```bash # opm (manual install libmaxminddb and download GeoLite2-City.mmdb) # openresty/openresty:alpine and apache/apisix:2.13.0-alpine docker image need to install perl libmaxminddb # e.g. apk --no-cache add perl libmaxminddb && ln -s /usr/lib/libmaxminddb.so.0 /usr/lib/libmaxminddb.so opm get anjia0532/lua-resty-maxminddb ``` -------------------------------- ### Formatted JSON Output Example Source: https://github.com/anjia0532/lua-resty-maxminddb/blob/master/README.md Presents a prettified JSON output of the MaxMind DB lookup results, showing the detailed geographical information for an IP address. ```json full: { "city": { "geoname_id": 1799962, "names": { "en": "Nanjing", "ru": "Нанкин", "fr": "Nankin", "pt-BR": "Nanquim", "zh-CN": "南京", "es": "Nankín", "de": "Nanjing", "ja": "南京市" } }, "subdivisions": [ { "geoname_id": 1806260, "names": { "en": "Jiangsu", "fr": "Province de Jiangsu", "zh-CN": "江苏省" }, "iso_code": "32" } ], "country": { "geoname_id": 1814991, "names": { "en": "China", "ru": "Китай", "fr": "Chine", "pt-BR": "China", "zh-CN": "中国", "es": "China", "de": "China", "ja": "中国" }, "iso_code": "CN" }, "registered_country": { "geoname_id": 1814991, "names": { "en": "China", "ru": "Китай", "fr": "Chine", "pt-BR": "China", "zh-CN": "中国", "es": "China", "de": "China", "ja": "中国" }, "iso_code": "CN" }, "location": { "time_zone": "Asia\/Shanghai", "longitude": 118.7778, "accuracy_radius": 50, "latitude": 32.0617 }, "continent": { "geoname_id": 6255147, "names": { "en": "Asia", "ru": "Азия", "fr": "Asie", "pt-BR": "Ásia", "zh-CN": "亚洲", "es": "Asia", "de": "Asien", "ja": "アジア" }, "code": "AS" } } node name: city, value: { "geoname_id": 1799962, "names": { "en": "Nanjing", ``` -------------------------------- ### Complete Nginx Example with MaxMindDB Source: https://github.com/anjia0532/lua-resty-maxminddb/blob/master/README.md Provides a full Nginx server configuration that integrates lua-resty-maxminddb. It handles incoming requests, performs IP lookups using either a provided IP or the client's IP, and returns the results in JSON format. ```nginx server { listen 80; server_name localhost; location / { content_by_lua_block{ local cjson = require 'cjson' local geo = require 'resty.maxminddb' if not geo.initted() then geo.init({ city = "/path/to/GeoLite2-City.mmdb", country = "/path/to/GeoLite2-Country.mmdb" }) end -- Lookup using default profile (first one) local res,err = geo.lookup(ngx.var.arg_ip or ngx.var.remote_addr) -- Multi database support -- local res,err = geo.lookup(ngx.var.arg_ip or ngx.var.remote_addr, nil, ngx.var.arg_type or 'city') if not res then ngx.log(ngx.ERR,'failed to lookup by ip ,reason:',err) end ngx.say("full :",cjson.encode(res)) if ngx.var.arg_node then ngx.say("node name:",ngx.var.arg_node," ,value:", cjson.encode(res[ngx.var.arg_node] or {})) end } } } ``` -------------------------------- ### Install lua-resty-maxminddb Source: https://github.com/anjia0532/lua-resty-maxminddb/blob/master/README.md This command installs the lua-resty-maxminddb library using luarocks, ensuring necessary build tools like perl and alpine-sdk are present. ```bash apk --no-cache add perl alpine-sdk && luarocks install lua-resty-maxminddb UNZIP=/usr/bin/unzip ``` -------------------------------- ### Install lua-resty-maxminddb via LuaRocks Source: https://github.com/anjia0532/lua-resty-maxminddb/blob/master/README.md Installs the lua-resty-maxminddb library using LuaRocks. Requires manual download of a GeoLite2 City database. Specific instructions are provided for different OpenResty Docker images. ```bash # luarocks (manual download GeoLite2-City.mmdb) # openresty/openresty:alpine docker image luarocks install lua-resty-maxminddb # openresty/openresty:alpine docker image need to install luarocks (ref https://github.com/openresty/docker-openresty/blob/master/alpine/Dockerfile.fat) # special apache/apisix:2.xx.0-alpine luarocks install lua-resty-maxminddb UNZIP=/usr/bin/unzip ``` -------------------------------- ### Nginx Request Examples and Output Source: https://github.com/anjia0532/lua-resty-maxminddb/blob/master/README.md Illustrates how to make requests to the Nginx server configured with lua-resty-maxminddb and shows the expected JSON output for IP lookups, including specific data nodes. ```bash #ipv4 $ curl localhost/?ip=114.114.114.114&node=city&type=city #ipv6 #$ curl localhost/?ip=2001:4860:0:1001::3004:ef68&node=country full :{"city":{"geoname_id":1799962,"names":{"en":"Nanjing","ru":"Нанкин","fr":"Nankin","pt-BR":"Nanquim","zh-CN":"南京","es":"Nankín","de":"Nanjing","ja":"南京市"}},"subdivisions":[{"geoname_id":1806260,"names":{"en":"Jiangsu","fr":"Province de Jiangsu","zh-CN":"江苏省"},"iso_code":"32"}],"country":{"geoname_id":1814991,"names":{"en":"China","ru":"Китай","fr":"Chine","pt-BR":"China","zh-CN":"中国","es":"China","de":"China","ja":"中国"},"iso_code":"CN"},"registered_country":{"geoname_id":1814991,"names":{"en":"China","ru":"Китай","fr":"Chine","pt-BR":"China","zh-CN":"中国","es":"China","de":"China","ja":"中国"},"iso_code":"CN"},"location":{"time_zone":"Asia\/Shanghai","longitude":118.7778,"accuracy_radius":50,"latitude":32.0617},"continent":{"geoname_id":6255147,"names":{"en":"Asia","ru":"Азия","fr":"Asie","pt-BR":"Ásia","zh-CN":"亚洲","es":"Asia","de":"Asien","ja":"アジア"},"code":"AS"}} node name:city ,value:{"geoname_id":1799962,"names":{"en":"Nanjing","ru":"Нанкин","fr":"Nankin","pt-BR":"Nanquim","zh-CN":"南京","es":"Nankín","de":"Nanjing","ja":"南京市"}} ``` -------------------------------- ### Run wrk Load Test Source: https://github.com/anjia0532/lua-resty-maxminddb/blob/master/docker/README.md This snippet shows how to execute a load test using the `wrk` tool within a Docker container. It mounts a Lua script (`wrk-test.lua`) for test configuration and targets a local OpenResty instance running on port 80. The output includes performance metrics for different OpenResty/libmaxminddb configurations. ```bash docker run -it --rm -v $(pwd)/wrk-test.lua:/tmp/wrk-test.lua --name wrk williamyeh/wrk:4.0.2 -t50 -c200 -d120s -s /tmp/wrk-test.lua --latency http://127.0.0.1:80 ``` -------------------------------- ### Download MaxMind Database Source: https://github.com/anjia0532/lua-resty-maxminddb/blob/master/docker/README.md This snippet demonstrates how to download the MaxMind database file using a Docker container. It requires an environment file (`geo-env`) containing license keys and account IDs. The downloaded data is stored in the `geoipupdate_data` directory. ```bash cd docker && mkdir -p geoipupdate_data cat geo-env # Change to your account GEOIPUPDATE_LICENSE_KEY GEOIPUPDATE_ACCOUNT_ID #vi geo-env docker run --env-file geo-env --rm --name geoipupdate -v $(pwd)/geoipupdate_data/:/usr/local/share/GeoIP/ ghcr.io/maxmind/geoipupdate # docker run --env-file geo-env --rm --name geoipupdate --entrypoint /bin/sh -it -v $(pwd)/geoipupdate_data/:/usr/local/share/GeoIP/ ghcr.io/maxmind/geoipupdate # HTTPS_PROXY=http://127.0.0.1:1080 HTTP_PROXY=http://127.0.0.1:1080 geoipupdate ``` -------------------------------- ### Basic MaxMindDB Lookup in Lua Source: https://github.com/anjia0532/lua-resty-maxminddb/blob/master/README.md Demonstrates the basic usage of lua-resty-maxminddb for performing a single IP address lookup. It initializes the library with a specified database file and then calls the lookup function. ```lua local geo = require 'resty.maxminddb' if not geo.initted() then geo.init("/path/to/GeoLite2-City.mmdb") end local res, err = geo.lookup("8.8.8.8") ``` -------------------------------- ### Multiple MaxMindDB Lookups in Lua Source: https://github.com/anjia0532/lua-resty-maxminddb/blob/master/README.md Shows how to initialize and use lua-resty-maxminddb with multiple MaxMind databases (e.g., city, country, ASN). It allows specifying which database to use for lookups. ```lua local geo = require 'resty.maxminddb' if not geo.initted() then geo.init({ city = "/path/to/GeoLite2-City.mmdb", country = "/path/to/GeoLite2-Country.mmdb", asn = "/path/to/GeoLite2-ASN.mmdb" }) end -- Lookup using default profile (first one) local res, err = geo.lookup("8.8.8.8") -- Or specify a specific profile local city_data, err = geo.lookup("8.8.8.8", nil, "city") local country_data, err = geo.lookup("8.8.8.8", nil, "country") local asn_data, err = geo.lookup("8.8.8.8", nil, "asn") ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.