美文网首页
lua - ipip地址库

lua - ipip地址库

作者: 路破格 | 来源:发表于2020-06-02 15:20 被阅读0次

目前仅支持IPv4地址查询,返回国家、省份、城市,不包含运营商

1、安装openresty

下载地址:http://openresty.org/cn/

安装示例:

wget https://openresty.org/download/openresty-1.15.8.3.tar.gz
tar zxvf openresty-1.15.8.3.tar.gz
cd openresty-1.15.8.3
./configure --prefix=/usr/local/openresty
gmake && gmake install

2、安装nginx-lua-ipdb扩展

下载地址:https://github.com/ipipdotnet/ipdb-luajit

安装示例:

wget https://github.com/ipipdotnet/ipdb-luajit/archive/master.zip
unzip master.zip
mv ipdb-luajit-master/resty/ipdb /usr/local/openresty/lualib/resty/

3 编写lua处理脚本

创建脚本目录

mkdir /usr/local/openresty/nginx/lua/

获取ipip试用版ipv4地址库:
下载地址:https://www.ipip.net/product/client.html
下载后的文件名为ipipfree.ipdb,优化建议可以把文件放到/dev/shm之类的内存盘下,提高查询速度

mv ipipfree.ipdb /usr/local/openresty/nginx/lua/
vim /usr/local/openresty/nginx/lua/ip.lua

local city = require 'resty.ipdb.city'
ipdb = city:new("/usr/local/openresty/nginx/lua/ipipfree.ipdb")
local ip = ngx.var.arg_ip

if ip
then
    local loc = ipdb:find(ip, "CN");
    -- 根据需要输出分隔符,本例使用\t\t分割字段,country_name为国家名,region_name为省份名,city_name为城市名
    ngx.say(loc.country_name .. "\t\t" .. loc.region_name .. "\t\t" .. loc.city_name);
else
    ngx.say("error")
end

5 配置nginx

vim /usr/local/openresty/nginx/conf.d/ip.conf

server {
    listen 80;
    server_name your_server_name;

    location / {
        default_type "text/html";
        charset utf-8;
        content_by_lua_file lua/ip.lua;
    }

    access_log off;
}

6 启动nginx

/usr/local/openresty/nginx/sbin/nginx -t && /usr/local/openresty/nginx/sbin/nginx

7 测试验证

curl "http://127.0.0.1/?ip=223.5.5.5"

相关文章

  • lua - ipip地址库

    目前仅支持IPv4地址查询,返回国家、省份、城市,不包含运营商 1、安装openresty 下载地址:http:/...

  • Lua与C交互简介

    Lua资源获取### Lua各个平台动态库与静态库下载地址Lua源码下载地址 交互原理#### 要理解Lua与C的...

  • lua - GeoIP地址库

    支持IPv4/IPv6地址查询,返回国家、省份、城市,不包含运营商 1、安装openresty 下载地址:http...

  • nginx(openrestry)实现动态负载均衡

    nginx配置 lua文件 相关Lua库 json解析库:https://github.com/mpx/lua-c...

  • lua获取utf8(包含中文字)字符串长度

    原文地址http://www.freecls.com/a/2712/e lua自带的string库计算字符串长度是...

  • Mac下Lua环境搭建

    lua官网http://www.lua.org 下载地址: http://www.lua.org/download...

  • Lua库函数概览

    Lua库函数概览数学库 table库 字符串库 IO库 os库 调试库

  • Prometheus监控Nginx

    环境 系统:CentOS 7.5 准备 Nginx添加Lua扩展 Nginx端 下载lua脚本地址:https:/...

  • lua在mac上的安装

    到官网安装了lua包,我安装的是lua-5.3.4 官网地址:http://www.lua.org/ 解压之后,命...

  • Lua C API

    C API 云风Blog:Lua C API 的正确用法 C读取和调用Lua文件的库:lua.h, lauxlib...

网友评论

      本文标题:lua - ipip地址库

      本文链接:https://www.haomeiwen.com/subject/fbmuzhtx.html