Записки криворукого админа
  • Записки криворукого админа
  • SaltStack
    • State
      • Name active interface
      • watch, require, *_in
    • Salt-api
      • tokens
    • ERROR
      • No module named tornado.stack_context
      • Got a bad pillar from master
  • Jinja
    • Last comma
  • Python
    • проверка версии модуля
  • Gitlab
    • Example values CI
    • gitlab multi pipeline
  • Linux
    • qemu/kvm
    • Cron all user
    • OpenVPN GUI
    • Group Linux
    • passwd
    • Systemctl
    • chmod для взрослых ;)
    • Comand0s
      • disck
      • grep
      • link
      • mkdir
    • Supervisor
    • Изменение hostname
    • WiFi - broadcom
    • cockpit
    • Viber
    • Время в терминале
    • Chmod
    • Docker ?
    • Тмух
    • Количество ядер
  • Utility
    • scp
      • zsh: no matches found
    • rsyslog
      • stop not work
    • Vi/Vim
      • Hotkeys
    • parted
      • Create over 2TB partition
    • lsyncd
    • mdadm
      • /boot on RAID1
  • MySQL
    • Типы таблиц (ENGINE)
    • Доступ для пользователя
    • Xtrabackup
    • WSREP - help
    • Примитивы
  • NetWork
    • OpenVPN
    • Установка Pure-FTP
  • Ansiblya
    • unmanaged files
  • Restore
    • Safest way to clean up boot partition
  • Редис
    • High Performance Redis
  • SSL
    • API cloudflare
    • bundel
  • Apache
    • Nginx, Apache, REMOTE_ADDR 127.0.0.1
  • Openresty/Nginx
    • default, Not $host
    • NGINX+Lua=parser_ip
    • NGINX+Lua=Size_img
  • Mong0
  • Mac
    • VirtualBox
  • Hobby
    • 🖖XMage
Powered by GitBook
On this page
  1. Openresty/Nginx

NGINX+Lua=parser_ip

Парсинг ip в nginx на лету по maxmind базе

apt install openresty-opm libmaxminddb-dev
opm get anjia0532/lua-resty-maxminddb
nginx.conf
  location /somemagicurl {
    expires max;
    content_by_lua_block {
    local cjson = require 'cjson'
    local geo = require 'resty.maxminddb'
    if not geo.initted() then
      geo.init("/geoip/GeoIP2-City.mmdb")
    end
    local res,err = geo.lookup(ngx.var.arg_ip or ngx.var.remote_addr)
    if not res then
      ngx.log(ngx.ERR,'failed to lookup by ip ,reason:',err)
    end
    if ngx.var.arg_node == nil then
      ngx.header["Content-Type"] = "application/json"
      ngx.say(cjson.encode(res))
      ngx.exit(0)
    else
      ngx.header["Content-Type"] = "application/json"
      ngx.say(cjson.encode(res[ngx.var.arg_node] or {}))
      ngx.exit(0)
    end
    }
  }

http://127.0.0.0.1/somemagicurl/

or

http://127.0.0.0.1/somemagicurl/?ip=8.8.8.8

or

http://127.0.0.0.1/somemagicurl/?ip=8.8.8.8&node=country

Previousdefault, Not $hostNextNGINX+Lua=Size_img

Last updated 5 years ago