nginx利用geoip模块屏蔽地区或国家访问
nginx的geoip说明和使用方法:
https://docs.nginx.com/nginx/admin-guide/security-controls/controlling-access-by-geoip/%20
下载geoip2的数据库
注册maxmind后下载数据库文件
然后,访问 Download Files 页面,以下载数据库文件:https://www.maxmind.com/en/accounts/current/geoip/downloads
下载GeoLite2-Country.mmdb和GeoLite2-City.mmdb,并放到/usr/local/nginx/geoip的目录中,并给权
在http段配置
geoip2 /usr/local/nginx/geoip/GeoLite2-Country.mmdb {
auto_reload 10m;
$geoip2_metadata_country_build metadata build_epoch;
$geoip2_data_country_code default=US source=$remote_addr country iso_code;
$geoip2_data_country_name country names en;
虚拟主机段配置,例不允许国内流量来访
if ($geoip_country_code = CN) {
deny all;
}
只允许国内流量来访
if ($geoip2_data_country_code != CN) {
return 200 $geoip2_data_country_code;
}
在每个虚拟主机的location前加入以上信息,当然你还可以加入location中
版权申明
本文系作者 @天边的云 原创发布在Loshub站点。未经许可,禁止转载。
暂无评论数据