MAXMIND 免费的GeoLite2数据库分享
· 技术积累 · Nginx Geoip

MAXMIND GeoLite2 ip地址数据库,更新时间2023-03-07,可结合nginx的geoip2模块实现用户的访问地区控制
GeoLite2 数据库是免费的 IP 地理定位数据库,与MaxMind 的 GeoIP2 数据库相比,但准确性较低。

数据库文件大小取决,官方对有些ip的添加和删除的,我使用中发现,最新的数据库文件解析不了的ip,在两年前的文件中可以解析?

官方网站:geoip2-services-and-databases

MAXMIND 免费的GeoLite2数据库分享

nginx规则示例: nginx要支持geoip2模块才可以使用

http{
···
    # 取值国家
    geoip2 /usr/local/nginx/geoip2/GeoLite2-Country.mmdb {
        $geoip2_country_code country names en;
    }

    # 取值省/市
    geoip2 /usr/local/nginx/geoip2/GeoLite2-City.mmdb {
        # 省
        $geoip2_sub subdivisions 0 names en;
        # 市
        $geoip2_city city  names en;
    }

    # 匹配市访问策略(默认拒绝,添加例外允许的)
    map $geoip2_city $allowed_city {
        default deny;
        Shanghai allow;
        Shenzhen allow;
        Zhengzhou allow;
    }

    # 匹配省访问策略(默认允许)
    map $geoip2_sub $allowed_sub {
        default allow;
    }

    # 匹配国家访问策略(默认拒绝,只允许中国的访问)
    map $geoip2_country_code $allowed_country {
        default deny;
        China allow;
    }


server {
    ······
    # 判断来源ip地区 为空的情况
    if ($geoip2_city = '') {
        # 如果数据库中没有对应的ip数据,就标记为deny值(拒绝访问)
        #set $allowed_city deny;
        # 如果数据库中没有对应的ip数据,就标记为unknown值
        set $geoip2_city unknown;
        # 如果数据库中没有对应的ip数据,自定义一个变量和值
        set $geoip2_city_tmp "地址库未识别";
    }
    # 判断来源ip省份 为空的情况
    if ($geoip2_sub = '') {
        # 如果数据库中没有对应的ip数据,就标记为temporary-allow值(允许访问)
        set $allowed_sub temporary-allow;
        set $geoip2_sub unknown;
        set $geoip2_sub_tmp "地址库未识别";
    }

    # 添加响应头 
    # 地区权限代码
    add_header client-access-city $allowed_city;
    # 国家权限代码
    add_header client-access-country $allowed_country;
    # 国家编码
    add_header client-country $geoip2_country_code;
    # 省
    add_header client-sub $geoip2_sub;
    # 市
    add_header client-city $geoip2_city;
    # 来源ip
    add_header client-ip $remote_addr;

    # 用户访问规则(可以根据国家 省份 市区做判断)

    # 匹配地区的值为deny的,禁止访问
    #if ( $allowed_city = deny ) { return 403; }

    # 匹配国家代码值为deny的,禁止访问
    #if ( $allowed_country = deny ) { return 403; }


    # 判断客户端ip地区,对应值为deny的重定向指定页面
    if ($allowed_city = deny) {
        rewrite ^(.*)$  /Accessdenied.html last;
        #return 403;
    }

    # 判断客户端ip对应的国家,对应值为deny的重定向指定页面
    if ( $allowed_country = deny ) {
        rewrite ^(.*)$  /Accessdenied.html last;
        #return 403;
    }


   ···
   }#server 结束
}#http 结束

数据库文件下载(蓝奏网盘): GeoLite2-City.mmdb | GeoLite2-Country.mmdb | 点我显示密码

使用命令查询ip在数据库中的地区和代码

mmdblookup -f ./GeoLite2-City.mmdb -i ip地址

本文最后更新时间 2023-05-10
文章链接地址:
https://me.jinchuang.org/archives/1329.html
本站文章除注明[转载|引用],均为本站原创内容,转载前请注明出处
Nginx 配置 Referer 防盗链
不允许空 Referer访问,可以使用反向代理解决
在Nginx中配置使用Geoip2模块
Filebeat Logstash elasticsearch Kibana 日志收集展示分析

SQLSTATE[HY000]: General error: Illegal mix of collations (utf8_general_ci,IMPLICIT) and (utf8mb4_general_ci,COERCIBLE) for operation 'like'

bin/ld: Dwarf Error: found dwarf version '5', this reader only handles version 2, 3 and 4 information.

我要留言