首页 > web > nginx

tengine/nginx 安装geoip2模块
时间:2021-12-15 17:32:00  点击: 来源:  作者:

geoip是一个可以屏蔽ip的nginx模块,需要自行编译安装,安装后可以屏蔽某些国家和城市的ip访问网站,对网站安全还是很重要的。 一:安装geoip2扩展依赖yum install libmaxminddb-devel -y#这个库是安装ngx_http_geoi ...

geoip是一个可以屏蔽ip的nginx模块,需要自行编译安装,安装后可以屏蔽某些国家和城市的ip访问网站,对网站安全还是很重要的。

 

一:安装geoip2扩展依赖

yum install libmaxminddb-devel -y

#这个库是安装ngx_http_geoip2_module模块必须有的

yum -y install epel-release jemalloc pcre* openssl* unzip wget zip

#安装其他nginx必须的库

 

二:下载ngx_http_geoip2_module模块

cd /usr/local/src

git clone https://github.com/leev/ngx_http_geoip2_module.git

wget http://mirror.cnop.net/web/module/ngx_http_geoip2_module-master.zip && unzip ngx_http_geoip2_module-master.zip

ls ngx_http_geoip2_module-master

三:编译nginx并检查模块是否安装成功

nginx建议选择最新版本。

useradd www #添加nginx运行用户

cd /home && wget https://mirror.cnop.net/web/tengine/tengine-2.3.3.tar.gz && tar zxvf tengine-2.3.3.tar.gz && cd tengine-2.3.3

#编译安装

./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module --add-module=./modules/ngx_http_upstream_vnswrr_module --add-module=./modules/ngx_http_concat_module --add-module=/usr/local/src/ngx_http_geoip2_module-master

注意:在编译时,我们只要加上 --add-module=/usr/local/src/ngx_http_geoip2_module-master这个参数(以实际下载到的路径为准)。

启动项大家可自行添加,方便以后快捷启动。

运行”nginx -m“ 命令, 发现 ”ngx_http_geoip2_module“ 说明成功。

四,下载ip库:模块安装成功后,还要在 Nginx 里指定数据库.

mkdir /usr/share/GeoIP && cd /usr/share/GeoIP && wget http://mirror.cnop.net/web/module/Geoip2.zip && unzip Geoip2.zip && rm -rf Geoip2.zip

 

说明:以上四步可通过下面项目中的Tengine一键安装脚本安装nginx和geoip模块,一键安装后从第五步手动操作即可:

https://gitee.com/cnop/shell

 

五,在http端中添加如下代码:

touch /usr/local/nginx/conf/ip.conf

#新建一个白名单文件,方便以后里面可以存放ip,Geoip 的白名单,支持 ip 段

vi /usr/local/nginx/conf/nginx.conf

在http 的”{ }“中加入以下:

#gzip on;

geoip2 /usr/share/GeoIP/GeoLite2-Country.mmdb {

$geoip2_data_country_code country iso_code;

}

map $geoip2_data_country_code $allowed_country {

default yes;

CN no;

}

#geoIP white

geo $remote_addr $ip_whitelist {

default 0;

include ip.conf;

}

站点的server段里面加一下代码拒绝所有不是国内ip:

server

{

listen 80;

server_name 0.0.0.0;

index index.php index.html index.htm default.php default.htm default.html;

root html;

if ($ip_whitelist = 1) {

break;

}

if ($allowed_country = yes)

{ return 403; }

location ~ ^/(/.user.ini|/.htaccess|/.git|/.svn|/.project|LICENSE|README.md)

{

return 404;

}

location ~ .*/.(gif|jpg|jpeg|png|bmp|swf)$

{

expires 30d;

error_log /dev/null;

access_log /dev/null;

}

location ~ .*/.(js|css)?$

{

expires 12h;

error_log /dev/null;

access_log /dev/null;

}

}

国内的ip可访问,国外直接返回403。

service nginx restart

这样就配置好了 nginx, 并且通过 GeoIP 限制了国家和城市的访问,并且支持白名单。

安装文档,附件: https://www.cnop.net/uploadfile/2021/1215/20211215175248117.pdf

”您可通过以下微信二维码,赞赏作者“
 
 
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表
推荐资讯
linux服务器同步互联网时间
linux服务器同步互联
亚马逊云lightsail服务器使用教程 | 亚马逊云lightsail使用指南
亚马逊云lightsail服
hostdare 4折优惠,直接降价60% 美国洛杉矶低至 $10.4/年 768M内存/1核/10gNVMe/500g流量
hostdare 4折优惠,直接
六类网线cat6a和cat6e的区别
六类网线cat6a和cat6e
相关文章
栏目更新
栏目热门

关于我们 | 广告服务 | 联系我们 | 网站地图 | 免责声明 | WAP | RSS


Copyright © 运维之家 2013-2023