宝塔安装nginx geoip2 扩展 并屏蔽指定国家ip请求 |
您所在的位置:网站首页 › nginx禁止国外ip › 宝塔安装nginx geoip2 扩展 并屏蔽指定国家ip请求 |
一,安装geoip2依赖扩展
yum install libmaxminddb-devel -y
二,下载ngx_http_geoip2_module
git clone https://github.com/leev/ngx_http_geoip2_module.git
三,nginx编译扩展
在安装nginx的时候选择编译安装,并手动增加扩展 nginx扩展安装好了之后实际上有一个默认的数据库,位置在/usr/share/GeoIP/ 目录下,我们需要使用自己下载的最新数据 https://geolite.clash.dev wget https://geolite.clash.dev/Country.mmdb这里只有国家数据,也可以自己到官网去下载 https://dev.maxmind.com/geoip/geolite2-free-geolocation-data, 五,配置nginx在nginx配置文件中,http段加入以下代码。以宝塔安装的nginx为例,打开/www/server/nginx/conf/nginx.conf文件,在http中添加 # 国家代码源 geoip2 /www/Country.mmdb { $geoip2_data_country_code country iso_code; } # 映射 MM map $geoip2_data_country_code $allowed_country { default yes; CN yes; }其中iso_code可参考 http://doc.chacuo.net/iso-3166-1/ 二位字母查询 修改 对应站点的配置文件,在/www/server/panel/vhost/nginx 目录中的网站配置文件的server段中添加 if ($allowed_country = no) { return 403; }重启nginx,然后根据不同国家ip网络测试即可。 六,参考文档https://www.cnblogs.com/aitansi/articles/14485883.html?ivk_sa=1024320u https://www.lmlphp.com/user/8336/article/item/378082/ https://me.jinchuang.org/archives/1169.html/comment-page-1 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |