Sign inSign up

mntu/openresty-geoip

By mntu

•Updated about 1 year ago

This is a docker image that uses OpenResty⁠ and maxmind⁠ GeoLite2 databse to provide a GeoIP API

Image
Networking
Web servers
Web analytics
0

840

mntu/openresty-geoip repository overview

⁠OpenResty GeoIP API

This is a docker image that uses OpenResty⁠ and maxmind⁠ GeoLite2 databse to provide a GeoIP API

⁠Build docker image
docker buildx build . --push --platform linux/arm64,linux/amd64 -t mntu/openresty-geoip

⁠Docker compose
version: "3.9"

services:
  openresty:
    image: mntu/openresty-geoip:latest
    restart: unless-stopped
    ports:
      - "8080:80"
    volumes:
      - ./nginx.conf:/usr/local/openresty/nginx/conf/nginx.conf:ro
      - ./data:/data
      - /etc/hosts:/etc/hosts:ro
    environment:
      - LICENSE_KEY=${LICENSE_KEY}

Get ${LICENSE_KEY} from maxmind⁠.

Copy .env.example file to .env and replace <YOUR_LICENSE_KEY> by LICENSE_KEY

⁠Sample configuration (nginx.conf)
worker_processes auto;

events { 
    worker_connections 8192; 
    use epoll;
    multi_accept on;
}

http {
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 15;
    keepalive_requests 10000;
    types_hash_max_size 4096;

    resolver 1.1.1.1 8.8.8.8 valid=300s;
    resolver_timeout 5s;

    access_log off;
    error_log off;

    client_body_buffer_size 64k;
    client_max_body_size 1m;
    gzip off;

    lua_package_path "/usr/local/share/lua/5.1/?.lua;/usr/local/share/lua/5.1/?/init.lua;/usr/local/openresty/lualib/?.lua;;";
    lua_package_cpath "/usr/local/lib/lua/5.1/?.so;/usr/local/openresty/lualib/?.so;;";

    init_by_lua_block {
        local maxminddb = require "resty.maxminddb"
        local ok, err = maxminddb.init({
            city = "/data/GeoLite2-City.mmdb"
        })
        if not ok then
            ngx.log(ngx.ERR, "failed to init maxminddb: ", err)
        else
            ngx.log(ngx.NOTICE, "maxminddb initialized successfully")
        end
        cjson = require "cjson"
    }

    server {
        listen 80;

        location /geo {
            content_by_lua_block {
                ngx.header.content_type = "application/json; charset=utf-8"
                local maxmind = require "resty.maxminddb"
                local ip = ngx.var.arg_ip
                if not ip then
                    local headers = ngx.req.get_headers()
                    local forwarded_for = headers["X-Forwarded-For"]
                    if forwarded_for then
                        ip = forwarded_for:match("([^,]+)")
                    else
                        ip = headers["X-Real-IP"] or ngx.var.remote_addr
                    end
                end
                local res, err = maxmind.lookup(ip, nil, 'city')

                if not res then
                    ngx.status = 404
                    ngx.say(cjson.encode({
                        ip = ip,
                        country = "Unknown",
                        city = "Unknown",
                        location = {},
                        error = err
                    }))
                    -- ngx.say("Lookup failed for IP ", ip, " error: ", err)
                    return
                end
                
                -- ngx.say(require("cjson").encode(res))
                ngx.say(cjson.encode({
                    ip = ip,
                    country = res.country and res.country.names and res.country.names.en or "",
                    city = res.city and res.city.names and res.city.names.en or "",
                    location = res.location or {}
                }))
            }
        }
    }
}

Tag summary

Content type

Image

Digest

sha256:e4306bde4…

Size

149.7 MB

Last updated

about 1 year ago

docker pull mntu/openresty-geoip