龙岩易富通网络科技有限公司

龙岩小程序开发,龙岩分销系统

debian系统宝塔nginx配置geoip2屏蔽国家访问

2025.10.31 | 31阅读 | 0条评论 | 通用代码

一:安装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;

}


赞 (

发表评论