全球主机交流论坛

标题: Nginx根据IP的国家、城市、ASN访问控制 [打印本页]

作者: 1121744186    时间: 2021-3-19 15:04
标题: Nginx根据IP的国家、城市、ASN访问控制

1、必要前提环境

NGINX
IP数据库
IP数据库下载地址:https://www.maxmind.com 登录后可以免费下载开源的数据库,效果还是很不错的,改网站也提供商业付费的数据库。

-> GeoLite2-ASN.mmdb
-> GeoLite2-City.mmdb
-> GeoLite2-Country.mmdb

ASN 相当于ip的品牌号码,可用来鉴别各个机房的ip,查询对应IDC机房的ASN可以到该网站:http://bgp.he.net

2、安装 libmaxminddb 用于查询 mmdb 数据库

yum -y install libmaxminddb-devel
3、测试工具

mmdblookup --file GeoLite2-Country.mmdb --ip=测试ip
4、安装 ngx_http_geoip2_module

cd /
git clone https://github.com/leev/ngx_http_geoip2_module
进入NGINX的源码目录下,宝塔的话是 /www/server/nginx/src ,

cd /www/server/nginx/src
nginx -V
nginx version: nginx/1.18.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.1.1g  21 Apr 2020
TLS SNI support enabled
configure arguments: --user=www --group=www --prefix=/www/server/nginx --add-module=/www/server/nginx/src/ngx_devel_kit --add-module=/www/server/nginx/src/lua_nginx_module --add-module=/www/server/nginx/src/ngx_cache_purge --add-module=/www/server/nginx/src/nginx-sticky-module --with-openssl=/www/server/nginx/src/openssl --with-pcre=pcre-8.43 --with-http_v2_module --with-stream --with-stream_ssl_module --with-stream_ssl_preread_module --with-http_stub_status_module --with-http_ssl_module --with-http_image_filter_module --with-http_gzip_static_module --with-http_gunzip_module --with-ipv6 --with-http_sub_module --with-http_flv_module --with-http_addition_module --with-http_realip_module --with-http_mp4_module --with-ld-opt=-Wl,-E --with-cc-opt=-Wno-error --with-ld-opt=-ljemalloc --with-http_dav_module --add-module=/www/server/nginx/src/nginx-dav-ext-module
复制 configure arguments:后面的全部内容,再最后面加上 --add-dynamic-module=/ngx_http_geoip2_module

./configure --user=www --group=www --prefix=/www/server/nginx --add-module=/www/server/nginx/src/ngx_devel_kit --add-module=/www/server/nginx/src/lua_nginx_module --add-module=/www/server/nginx/src/ngx_cache_purge --add-module=/www/server/nginx/src/nginx-sticky-module --with-openssl=/www/server/nginx/src/openssl --with-pcre=pcre-8.43 --with-http_v2_module --with-stream --with-stream_ssl_module --with-stream_ssl_preread_module --with-http_stub_status_module --with-http_ssl_module --with-http_image_filter_module --with-http_gzip_static_module --with-http_gunzip_module --with-ipv6 --with-http_sub_module --with-http_flv_module --with-http_addition_module --with-http_realip_module --with-http_mp4_module --with-ld-opt=-Wl,-E --with-cc-opt=-Wno-error --with-ld-opt=-ljemalloc --with-http_dav_module --add-module=/www/server/nginx/src/nginx-dav-ext-module --add-dynamic-module=/ngx_http_geoip2_module

make
然后在当前目录的 objs 目录下面得到编译好的 ngx_http_geoip2_module.so

nginx.conf

load_module /modules/ngx_http_geoip2_module.so;

http
{
        geoip2 /www/ipdb/GeoLite2-Country.mmdb {
          auto_reload 60m;
          $geoip2_metadata_country_build metadata build_epoch;
          $geoip2_data_country_code default=CN source=$remote_addr country iso_code;
          $geoip2_data_country_name country names en;
        }

        geoip2 /www/ipdb/GeoLite2-City.mmdb {
          $geoip2_data_city_name default=null city names en;
        }
        geoip2 /www/ipdb/GeoLite2-ASN.mmdb {
          $geoip2_data_asn_code default=0 autonomous_system_number;
          $geoip2_data_asn_name default=null autonomous_system_organization;
        }
    //......省略
}
默认是使用 $remote_addr ,一般反向代理是 X-Forwarded-For ,CF 是 CF-Connecting-IP,需要自定义 使用 source=$remote_addr 设置。

5、测试展示

location /ip
{
  default_type    text/html;
  return 200 '$geoip2_data_country_code $geoip2_data_country_name  $geoip2_data_city_name  $geoip2_data_asn_name $geoip2_data_asn_code';
}

location /
{
    if ($geoip2_data_country_name = "Japan") {
        return 444;
    }
    //....省略
}
最后解释下 $geoip2_data_country_name country names en;

country names en 就是结果的JSON结构,举一反三使用 mmdblookup 工具可以查看数据内容。

{
    country:{
        names:{
            en:"US"
        }
    }

}


格式化不是很好,具体到 我的博客上看吧  https://lmcw.cn/174.html

作者: CCCP    时间: 2021-3-19 15:27
帮顶
作者: 我是坏虫    时间: 2021-3-19 15:28
提示: 作者被禁止或删除 内容自动屏蔽
作者: hardwar    时间: 2021-3-19 15:34
这个库不用注册 方便点
https://db-ip.com/db/lite.php
作者: 百度网盘    时间: 2021-3-19 15:36
支持技术贴,绑定
作者: lhspang    时间: 2021-3-19 15:38

支持技术贴,绑定
作者: 3333    时间: 2021-3-19 15:42
提示: 作者被禁止或删除 内容自动屏蔽
作者: sdqu    时间: 2021-3-19 15:47
我倒是想用这个库来做个dnspod的仿站
作者: zhongziso    时间: 2021-3-19 16:44
mark
作者: imslc    时间: 2021-3-19 16:56
好麻烦啊,还是cf方便些
作者: micto    时间: 2021-3-19 17:00
imslc 发表于 2021-3-19 16:56
好麻烦啊,还是cf方便些

是的,CF可以透传给后端国家代码。
不过CF貌似对百度搜索不友好,我的一个小破站因为套了CF,就收录了个位数,半年多了。
作者: alivefox    时间: 2021-3-19 17:02
战略mark
作者: imslc    时间: 2021-3-19 17:26
micto 发表于 2021-3-19 17:00
是的,CF可以透传给后端国家代码。
不过CF貌似对百度搜索不友好,我的一个小破站因为套了CF,就收 ...

百度跟cf结仇了,基本上套cf的站收录都不好,建议用个dnspod解析,把百度的蜘蛛回源.
作者: 1121744186    时间: 2021-3-19 17:42
micto 发表于 2021-3-19 17:00
是的,CF可以透传给后端国家代码。
不过CF貌似对百度搜索不友好,我的一个小破站因为套了CF,就收 ...

套CF速度就很拉胯了
作者: sojurice    时间: 2022-2-27 14:28
技术贴,支持
作者: blacklife    时间: 2022-2-28 00:21
技术贴M了
作者: 88170351    时间: 2022-2-28 11:10
技术贴
作者: 采虚昆    时间: 2022-3-1 11:28
帮顶
作者: 飞燕    时间: 2022-4-25 12:03
好麻烦啊,还是cf方便些




欢迎光临 全球主机交流论坛 (https://www.91ai.net/) Powered by Discuz! X3.4