ipdb和geoip2模块非常相似,前者为国内公司做的ip地址库,后者为国外搞的。不过免费版都不是非常精准。
模块地址:https://github.com/vislee/ngx_http_ipdb_module
[root@MiWiFi-RA69-srv ~]# yum install json-c json-c-devel nginx编译时加上参数: --add-module=/home/ngx_http_ipdb_module-master
去ipip.net下载免费版的地址库文件(https://www.ipip.net/product/ip.html#ipv4city)
最后更新时间是2019年7月,版本还是挺老了。
以下部分在http{}段配置:
ipdb /home/www/ipipfree.ipdb; ipdb_language CN; # 定义map map $ipdb_city_name $allowed_city { default yes; "北京" no; "上海" no; }
以下部分在server{}段配置:
server { listen 80; server_name www.test.com; index index.html; root /home/www/www.test.com; location / { if ($allowed_city = "no") { return 403; } } # 提供一个查询接口 location /ip { # ipdb_specifies_addr $http_addr; # ipdb_language EN; default_type application/html; add_header Content-Type "text/plain;charset=utf-8"; return 200 "country_name:$ipdb_country_name, city_name:$ipdb_city_name,raw_info:$ipdb_raw"; } access_log off; error_log /dev/null; }
关于模块中更多的内置变量,可以去看github中文档。
原创文章,转载请注明。本文链接地址: https://www.rootop.org/pages/4990.html