最新宝塔编译模块ngx_http_geoip2 禁止特定国家IP访问
at 4个月前 ca 记录笔记 by authorhu
Nginx + geoip2很强大 记录下编译流程
此方法需要卸载宝塔原有nginx重新安装
一:安装geoip2依赖
yum install libmaxminddb-devel -y
如果安装依赖失败请跳转本文末。
二:下载ngx_http_geoip2_module
cd /tmp git clone https://github.com/leev/ngx_http_geoip2_module.git cd ngx_http_geoip2_module ls
ls有确认5个文件 config LICENSE ngx_http_geoip2_module.c ngx_stream_geoip2_module.c README.md
三:打开宝塔面板 商店 安装NGINX 已安装请卸载。
--add-module=/root/ngx_http_geoip2_module
等待编译完成
四:下载数据库
官方需要注册登录后下载:https://www.maxmind.com/en/accounts/437377/geoip/downloads
度盘: https://pan.baidu.com/s/1QrhfiSs36udDXUDeqLjzCg 提取码: eezb
下载后放到/www/server/nginx/ 路径可随意 下一步配置文件会用到
五:修改 nginx 配置
默认修改 /www/server/nginx/conf/nginx.conf
在http{ 下加入
geoip2 /www/server/nginx/GeoLite2-Country.mmdb { $geoip2_data_country_code country iso_code; } map $geoip2_data_country_code $allowed_country { default no; CN yes; #中国大陆 TW yes; #允许台湾,台湾也是中国的 HK yes; #中国香港 MO yes; #中国澳门 }
六:配置到站点
修改站点配置文件的server段里面加一下代码才能生效
if ($allowed_country = no) { return 403; }
重启 NGINX
//end
如第一步yum安装依赖库失败,CentOS 7 安装 libmaxminddb
详见
https://www.24kplus.com/linux/608.html
版权声明
本文仅代表作者观点,未经许可,不得转载。