欢迎来到梦飞科技

服务器租用

当前优惠活动:

nginx利用GeoIP模块克制指定地域的用户会见会见

本文先容如何利用GeoIP模块nginx实现限制某个地域用户会见的成果。nginx要加上 --with-http_geoip_module 参数举办编译。

1、首先我们查抄一下nginx是否编译了GeoIP模块

nginx -V

假如你在输出界面看到了 --with-http_geoip_module,那么就说明nginx已经编译了GeoIP模块。

2、接下来我们安装GeoIP数据库
在Debian/Ubuntu系统,我们可以执行下面的呼吁举办安装:

apt-get install geoip-database libgeoip1

安装完成之后,GeoIP数据库会被安装在 /usr/share/GeoIP/GeoIP.dat。

这个GeoIP.dat是GeoIP数据库文件,利用apt-get呼吁安装的话这个文件不是最新的,我们可以从 http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz 这里下载最新的GeoIP数据库文件。

mv /usr/share/GeoIP/GeoIP.dat /usr/share/GeoIP/GeoIP.dat_bak

cd /usr/share/GeoIP/
wget
http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
gunzip GeoIP.dat.gz

3、此刻来设置nginx.conf文件

vi /etc/nginx/nginx.conf

将下面的内容添加进 http {} 区域,而且要放在任何 include 语句之前。

geoip_country /usr/share/GeoIP/GeoIP.dat;
map $geoip_country_code $allowed_country {
default yes;
FK no;
FM no;
EH no;
}

上面这些语句是除了 FK,新加坡云主机 香港云主机,FM,EH这三个地域用户答允其它地域的用户会见。

也可以只答允部门地域用户会见:

geoip_country /usr/share/GeoIP/GeoIP.dat;
map $geoip_country_code $allowed_country {
default no;
FK yes;
FM yes;
EH yes;
}

上面这些语句是除了 FK,FM,EH这三个地域的用户其它地域的用户都不答允会见。

上面的语句只是配置了一个 $allowed_country 变量,要最终实现克制配置的地域用户会见,我们要对 $allowed_country 变量举办判定处理惩罚。
在 server {} 区域里添加以下内容:

if ($allowed_country = no) {
return 403;
}

也可以针对某个特定url举办限制:

location /special {
if ($allowd_country = no) {
return 403;
}
}

4、重启nginx

/etc/init.d/nginx reload

这样我们就实现了nginx限制某个地域用户会见的成果。

梦飞科技 - 全球数据中心基础服务领先供应商

Copyright © 2003-2019 MFISP.COM. 国外服务器租用 IDC公司 版权所有 粤ICP备11019662号