一:安装geoip2依赖扩展
sudo apt install libmaxminddb-dev -y
二:下载ngx_http_geoip2_module
git clone https://github.com/leev/ngx_http_geoip2_module.git
三:在宝塔面板里边安装nginx的时候选择编译安装,并手动增加扩展,安装时候点击启用
模块名称 ngx_http_geoip2_module
模块说明 禁用国家ip
模块参数 --add-module=/path/to/ngx_http_geoip2_module
等待安装后输入 nginx -V 查看是否安装成功
四:在宝塔中安装geoip
配置nginx
在nginx配置文件中,http段加入以下代码。以宝塔安装的nginx为例,打开/www/server /nginx/conf/nginx.conf文件,
在http中添加
geoip2 /usr/share/GeoIP/Country.mmdb {
$geoip2_data_country_code country iso_code;
}
map $geoip2_data_country_code $allowed_country {
default yes;
CN yes;
JP no;
DE no;
}
在站点配置文件添加:
if ($allowed_country = no) {
return 403;
}
发表评论