为了实现业务系统针对不同地区IP访问,展示包含不同地区信息的业务交互界面。很多情况下系统需要根据用户访问的IP信息,判断用户可能的访问区域,针对不同的区域提供个性化的服务内容。本方案在CentOS7.x环境下基于高性能的Openresty1.13.6.2来实现。

方案介绍

要通过IP地址确认归属地,通常可以使用一些在线查询服务来实现,比如https://blog.csdn.net/XinTeng2012/article/details/34418117?utm_source=blogxgwz8 介绍的常见提供查询服务。 但使用在线服务查询潜在存在性能问题,同时通过lua来访问外部服务增加额外的代码量。 通过本地的GeoIP库来实现查询是个比较好的方案,GeoIP提供免费和收费服务(https://www.maxmind.com/en/home),大多数情况下使用定期更新的GeoIP数据库能满足基本需求。

环境准备

一 OpenResty安装

OpenResty方便地将Nginx和常用的各类lua库打包发布,可以方便地参考 https://openresty.org/en/installation.html 文档从源码编译安装。主要安装步骤说明如下:

tar -xvf openresty-VERSION.tar.gz
cd openresty-VERSION/
./configure -j2 --prefix=/usr/local/openresty
make -j2
sudo make install# better also add the following line to your ~/.bashrc or ~/.bash_profile file.
export PATH=/usr/local/openresty/bin:$PATH

这里的VERSION 是OpenResty具体版本号,目前为 1.13.6.2。编译安装后可以通过如下命令查看版本信息:

/usr/local/openresty/bin/openresty -V
nginx version: openresty/1.13.6.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC)?
built with OpenSSL 1.0.2k-fips ?26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/openresty/nginx --with-cc-opt=-O2 --add-module=../ngx_devel_kit-0.3.0 --add-module=../echo-nginx-module-0.61 --add-module=../xss-nginx-module-0.06 --add-module=../ngx_coolkit-0.2rc3 --add-module=../set-misc-nginx-module-0.32 --add-module=../form-input-nginx-module-0.12 --add-module=../encrypted-session-nginx-module-0.08 --add-module=../srcache-nginx-module-0.31 --add-module=../ngx_lua-0.10.13 --add-module=../ngx_lua_upstream-0.07 --add-module=../headers-more-nginx-module-0.33 --add-module=../array-var-nginx-module-0.05 --add-module=../memc-nginx-module-0.19 --add-module=../redis2-nginx-module-0.15 --add-module=../redis-nginx-module-0.3.7 --add-module=../rds-json-nginx-module-0.15 --add-module=../rds-csv-nginx-module-0.09 --add-module=../ngx_stream_lua-0.0.5 --with-ld-opt=-Wl,-rpath,/usr/local/openresty/luajit/lib --with-stream --with-stream_ssl_module --with-http_ssl_module

openresty包含了自身的包维护工具opm,该工具采用perl实现依赖MD5,需要执行yum install -y perl-Digest-MD5 安装。

yum install  -y perl-Digest-MD5

二:GeoIP2安装

2.1 从https://dev.maxmind.com/geoip/geoip2/geolite2/ 下载MaxMind格式的GeoIP2数据库保存到本地服务器。
比如将数据库文件GeoLite2-City.mmdb保存到/usr/local/openresty目录下。

资料参考地址:免费账户注册地址:https://www.maxmind.com/en/geoip2-country-databaseGeoLite2 免费地理定位数据【此地址去获取GEOIP数据】:https://dev.maxmind.com/geoip/geolite2-free-geolocation-data官方 API 客户端:https://dev.maxmind.com/geoip/geolocate-an-ip/databaseslua客户端地址:https://github.com/daurnimator/mmdblua使用说明(更新 GeoIP 和 GeoLite 数据库):https://dev.maxmind.com/geoip/updating-databases?lang=en#3-run-geoip-updateGeoIP2 和 GeoLite2 数据库文档:https://dev.maxmind.com/geoip/docs/databases?lang=en各自使用各自注册用户的下载地址:下载地址:https://www.maxmind.com/en/accounts/580484/geoip/downloads

GeoLite2 免费地理定位数据【此地址去获取GEOIP数据】:https://dev.maxmind.com/geoip/geolite2-free-geolocation-data

[root@localhost solf]# ls
GeoLite2-Country_20210713         libmaxminddb-1.6.0         openresty-1.13.6.2
GeoLite2-Country_20210713.tar.gz  libmaxminddb-1.6.0.tar.gz  openresty-1.13.6.2.tar.gz
[root@localhost solf]# cd GeoLite2-Country_20210713
[root@localhost GeoLite2-Country_20210713]# ls
COPYRIGHT.txt  GeoLite2-Country.mmdb  LICENSE.txt
[root@localhost GeoLite2-Country_20210713]# mv GeoLite2-Country.mmdb  /usr/local/openresty/
[root@localhost GeoLite2-Country_20210713]# ls /usr/local/openresty/
bin  COPYRIGHT  GeoLite2-Country.mmdb  luajit  lualib  nginx  pod  resty.index  site

2.2 GeoIP2 lua库安装,GeoIP2 lua库位于https://github.com/anjia0532/lua-resty-maxminddb ,可以通过如下命令方便安装:

扩展地址:https://github.com/anjia0532/lua-resty-maxminddb

/usr/local/openresty/bin/opm get anjia0532/lua-resty-maxminddb

2.3 GeoIP2 lua库依赖动态库安装,lua库依赖libmaxminddb实现对mmdb的高效访问。需要编译该库并添加到openresty访问环境。可以从https://github.com/maxmind/libmaxminddb/releases下载相应源码包到本地编译部署。基本编译步骤如下:

[root@localhost solf]# pwd
/usr/local/solf
[root@localhost solf]# wget https://github.com/maxmind/libmaxminddb/releases/download/1.6.0/libmaxminddb-1.6.0.tar.gz
[root@localhost solf]# tar -zxvf libmaxminddb-1.6.0.tar.gz
[root@localhost solf]# cd libmaxminddb-1.6.0
[root@localhost libmaxminddb-1.6.0]# ./configure
[root@localhost libmaxminddb-1.6.0]# make
[root@localhost libmaxminddb-1.6.0]# make check
[root@localhost libmaxminddb-1.6.0]# make install
[root@localhost libmaxminddb-1.6.0]# ldconfig

默认情况下上述操作会将libmaxminddb.so部署到/usr/local/lib目录下,为了让openresty访问,可以拷贝到openresty目录下,或通过如下步骤更新ldconfig。

[root@localhost libmaxminddb-1.6.0]# sudo sh -c "echo /usr/local/lib  >> /etc/ld.so.conf.d/local.conf"
[root@localhost libmaxminddb-1.6.0]# ldconfig
[root@localhost libmaxminddb-1.6.0]# ls /usr/local/lib
libmaxminddb.a  libmaxminddb.la  libmaxminddb.so  libmaxminddb.so.0  libmaxminddb.so.0.0.7  pkgconfig

三:配置openresty nginx环境。

1,配置openresty nginx加载相应的lua库和动态库,需要在http段添加如下指令,其中的;;表示默认库路径:

    lua_package_path  "/usr/local/openresty/lualib/?.lua;;";lua_package_cpath  "/usr/local/openresty/lualib/?.so;;";

2,指定lua处理请求的方式。 为了简易直观,如下示例的nginx.conf配置指定 /lua开始的url请求通过 /usr/local/openresty/lualib/project/test.lua脚本来处理,这里没有做其他复杂的请求和变量处理工作。 现网环境下可能需要考虑更好的模块化配置管理方式,lua_code_cache off;参数只为测试使用,生产环境需设为on;

lua_package_path  "/usr/local/openresty/lualib/?.lua;;";
lua_package_cpath  "/usr/local/openresty/lualib/?.so;;";include       mime.types;
default_type  application/octet-stream;server {listen       80;server_name  localhost;location / {root   html;index  index.html index.htm;}location /lua {default_type "text/html";proxy_set_header            X-real-ip $remote_addr;proxy_set_header            X-Forwarded-For $proxy_add_x_forwarded_for;#return 200 $proxy_add_x_forwarded_for;charset utf-8;#关闭代码缓存 。修改lua脚本不需要重启lua_code_cache off;content_by_lua_file  /usr/local/openresty/lualib/project/test.lua;}
}

四:编写访问mmdb的lua脚本

我们想通过传入IP地址的方式来查看归属地城市,比如,http://192.168.5.101/lua?ip=111.47.224.182&node=city。 可以如下方式编写按照上面设置的conf/lua/test.lua。

[root@localhost project]# pwd
/usr/local/openresty/lualib/project
[root@localhost project]# vim test.lua
local cjson=require 'cjson'
local geo=require 'resty.maxminddb'
local arg_ip=ngx.var.arg_ip
--local arg_node=ngx.var.arg_node
--ngx.say("IP:",arg_ip,", node:",arg_node,"<br>")--没有传递ip
if not arg_ip thenarg_ip=ngx.var.remote_addr
endif not geo.initted() thengeo.init("/usr/local/openresty/GeoLite2-Country.mmdb")
end--当前服务部署的ip
--ngx.say("当前服务部署ip:",ngx.var.remote_addr)local res,err=geo.lookup(arg_ip or ngx.var.remote_addr)if not res thenngx.say("获取客户端ip失败,或当前请求的ip不是公网ip")ngx.log(ngx.ERR,' failed to lookup by ip , reason :',err)
elsefor k,v in pairs(res) do--只获取国家if(k == "country") then--获取国家编码for key,item in pairs(v) doif (key=="iso_code") then--ngx.say(item)endendendend--ngx.say("Result:",cjson.encode(res))ngx.say(cjson.encode(res))if arg_node thenngx.say("node name:",ngx.var.arg_node, " , value:",cjson.encode(res[ngx.var.arg_node] or {}))endendngx.exit(200)

五:访问验证

浏览器访问:http://192.168.5.101/lua?ip=2.56.135.255

{"country": {"geoname_id": 2921044,"names": {"en": "Germany","ru": "Германия","fr": "Allemagne","pt-BR": "Alemanha","zh-CN": "德国","es": "Alemania","de": "Deutschland","ja": "ドイツ連邦共和国"},"iso_code": "DE","is_in_european_union": true},"continent": {"geoname_id": 6255148,"names": {"en": "Europe","ru": "Европа","fr": "Europe","pt-BR": "Europa","zh-CN": "欧洲","es": "Europa","de": "Europa","ja": "ヨーロッパ"},"code": "EU"},"registered_country": {"geoname_id": 2921044,"names": {"en": "Germany","ru": "Германия","fr": "Allemagne","pt-BR": "Alemanha","zh-CN": "德国","es": "Alemania","de": "Deutschland","ja": "ドイツ連邦共和国"},"iso_code": "DE","is_in_european_union": true}
}

Openresty + GeoIP2实现IP归属国家查询相关推荐

  1. IP 归属地查询 API 教你从0到1顺着网线找到键盘侠

    写在前面 前一段时间,微博.头条.抖音.知乎等各大互联网平台相继推出了"个人主页.评论显示 IP 属地,用户不能自己开启或关闭"的功能,那么这些平台是如何获取到用户的 IP 地址呢 ...

  2. IP归属地查询(基于本地IP库实现)

    这是一篇GitHub上的项目说明文档,项目地址: Zzz2333/IPLibrary 本项目功能是根据IP查询归属地.用到了纯真IP地址数据库,即qqwry.dat文件. 纯真IP地址数据库是一个网上 ...

  3. python爬取免费优质IP归属地查询接口

    python爬取免费优质IP归属地查询接口 python爬取免费优质IP归属地查询接口 具体不表,我今天要做的工作就是: 需要将数据库中大量ip查询出起归属地 刚开始感觉好简单啊,毕竟只需要从百度找个 ...

  4. 全球IP归属地查询 IP地址查询

    全球IP归属地查询 IP地址查询 推荐一个非常简单的全球IP归属地查询 IP地址查询接口,只需要传入要查询的IP,即可返回相应的基本信息,以下是演示 请求地址是:http://ip.wyk-super ...

  5. 免费IP归属地查询接口

    免费IP归属地查询接口 淘宝API接口 IP-API接口 太平洋API接口 淘宝API接口 接口文档 http://ip.taobao.com/instructions 调用示例 GET http:/ ...

  6. 如何在Excel/WPS表格中实现IP归属地查询?

    给您一个IP地址,您能查出它的归属地是哪座城市吗?今天小编就教给大家如何在Excel/WPS表格中使用IP归属地查询公式.使用公式前需要去腾讯位置服务申请key和Secret key,网址:腾讯位置服 ...

  7. Python/PHP:免费IP归属地查询接口和通过ip获取大致位置信息

    1.淘宝API接口: 文档:http://ip.taobao.com/instructions 示例: GET http://ip.taobao.com/outGetIpInfo?ip=221.218 ...

  8. php查询ip归属地api接口_php调用新浪接口查询ip获取地理位置(ip归属地查询)

    php调用新浪接口查询ip获取地理位置(ip归属地查询) 发布于 2014-11-17 08:35:58 | 115 次阅读 | 评论: 0 | 来源: 网友投递 PHP开源脚本语言PHP(外文名: ...

  9. 05全球IP归属地查询工具

    # 写在前面,这篇文章的原创作者是Charles我只是在他这个程序的基础上边进行加工,另外有一些自己的改造# 并都附上了注释和我自己的理解,这也是我一个学习的过程.# 附上大佬的GitHub地址:ht ...

  10. 【Python爬虫案例学习9】python爬取免费优质IP归属地查询接口

    刚开始感觉好简单啊,毕竟只需要从百度找个免费接口然后来个python脚本跑一晚上就ok了嘛~但是,网上免费接口要么限制访问频率(淘宝的),要么限制访问次数(百度及其他) 没辙了,从百度找到了几个在线查 ...

最新文章

  1. 类项目中的配置文件app.config在打包安装后的信息获取的问题
  2. 高并发Nginx,Tomcat8,mysql数据库配置
  3. 阿里软件测试工程师手把手教学——如何更好地设计测试用例?
  4. 微信支付-接收返回通知结果
  5. 【数据平台】sklearn库特征工程之特征选择和降维
  6. 4、C语言面试笔试--内存操作-指针
  7. Python小知识: List的赋值方法,不能直接等于
  8. ieda 远程调试hive_idea 远程调试
  9. python数据分析实战案例-Python数据分析案例实战
  10. 程序员过关斩将--为微服务撸一个简约而不简单的配置中心
  11. Hello World with Spring 3 MVC
  12. react 布局容器_如何使用容器模式开发React超能力
  13. boost::asio的C/S结构笔记
  14. Springboot2.0从零开始搭建脚手架-初始化和整合MybatisPlus3.0+...
  15. 聪明人:三不管,四不说,五不帮!
  16. python负数错误异常类型_十七、深入Python异常处理
  17. 摩莎485通讯测试软件,485串口测试软件1.6 免费版
  18. 高等数学——变限积分求导
  19. 〔转载〕20条欠揍短消息
  20. 跨性別/偽娘/性轉漫畫中譯對照

热门文章

  1. 大器晚成总比一事无成要强
  2. 第十一届蓝桥杯b组(10月真题)
  3. mac如何安装vue(简单明了)
  4. android 百度地图鹰眼,百度地图鹰眼轨迹
  5. 互联网时代成功创业故事
  6. shim和polyfill
  7. 美利财务平台架构演进
  8. BUUCTF 小易的U盘
  9. write-through与write-back的区别
  10. 基于Python语言的文件与文件夹管理