admin管理员组文章数量:1567736
一、安装 geoip2 扩展依赖
## 编译安装
cd /usr/local/src
wget https://linuxhy.top/pptp/Nginx_Geoip2/libmaxminddb-1.6.0.tar.gz
tar xf libmaxminddb-1.6.0.tar.gz
cd libmaxminddb-1.6.0/
./configure && make && make install
echo "/usr/local/lib" >> /etc/ld.so.conf
ldconfig
或者使用yum安装
yum install libmaxminddb-devel -y
二、下载 ngx_http_geoip2_module 模块
cd /usr/local/src
wget https://linuxhy.top/pptp/Nginx_Geoip2/3.3.tar.gz
tar xf 3.3.tar.gz
mv ngx_http_geoip2_module-3.3 ngx_http_geoip2_module
或者在github下载
git clone https://github/leev/ngx_http_geoip2_module.git
三、安装 nginx 模块
1、安装依赖
yum -y install pcre-devel openssl-devel perl-devel libxml2
2、下载编译安装
cd /usr/local/src
wget https://nginx/download/nginx-1.20.2.tar.gz
tar xf nginx-1.20.2.tar.gz
cd nginx-1.20.2
./configure --prefix=/usr/local/nginx --user=root --group=root --with-file-aio --with-pcre --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-http_ssl_module --with-stream --add-module=/usr/local/src/ngx_http_geoip2_module
make
make install
扩展:
如果用
./configure ... --add-dynamic-module=/usr/local/src/ngx_http_geoip2_module
后面在 nginx.conf 文件的http中,需要添加 load_module /usr/lib64/nginx/modules/ngx_http_geoip2_module.so ;
备注:如果是重新编译nginx,只需要到make
这一步,不需要执行make install
,make好以后,把 objs/nginx 拷贝到 /usr/local/nginx/sbin/nginx
,重新启动即可。
四、下载最新的 IP 地址数据库文件
cd /usr/local/nginx/conf/
mkdir geoip2
cd geoip2
wget https://linuxhy.top/pptp/Nginx_Geoip2/GeoLite2-master/GeoLite2-Country.mmdb
五、添加geoip访问策略
1、在 http 中添加 几行,定义数据库文件位置
http {
....
geoip2 /usr/local/nginx/conf/geoip2/GeoLite2-Country.mmdb {
auto_reload 5m ;
$geoip2_country_code country iso_code;
}
map $geoip2_country_code $allowed_country {
default yes;
CN no;
}
....
}
2、在server中添加
#前端Nginx配置页面必须添加.
server {
listen 80;
server_name xxx.xxx
....
# 匹配国家代码定义为no的,禁止访问,返回403(上面定义中国的 iso_code 为no)
if ( $allowed_country = no ) { return 403; }
# 403错误跳转至指定二级页面
error_page 404 403 500 = https://error.xxx/;
....
}
六、重启nginx,并测试
pkill nginx
/etc/init.d/nginx start
版权声明:本文标题:nginx禁止国内IP访问网站 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/dongtai/1726396742a1068868.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论