Nginx使用GeoIP限制国家访问
安装Geoip库
安装完成之后,GeoIP数据库会被安装在/usr/share/GeoIP/GeoIP.dat
.
[root@vultr ~]# yum -y install geoip-devel
[root@vultr openresty-1.13.6.1]# ls /usr/share/GeoIP/GeoIP.dat
/usr/share/GeoIP/GeoIP.dat
安装openresty
[root@vultr openresty-1.13.6.1]# ./configure --user=www --group=www --prefix=/usr/local --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-ipv6 --with-http_sub_module --with-http_geoip_module
[root@vultr openresty-1.13.6.1]# make && make install
配置openresty支持Geoip
[root@vultr openresty-1.13.6.1]# vim /usr/local/nginx/conf/nginx.conf
....
http {
geoip_country /usr/share/GeoIP/GeoIP.dat;
map $geoip_country_code $allowed_country {
default yes;
US no; # 国家 no就是不允许哪个国际访问
}
....
server{
....
if ($allowed_country = no) { # 添加判断,如果访问国家=no,就返回404
return 404;
}
...
}
[root@vultr openresty-1.13.6.1]# /usr/local/nginx/sbin/nginx -t # 检测nginx配置文件
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@vultr openresty-1.13.6.1]# /usr/local/nginx/sbin/nginx # 启动nginx
测试访问效果
参考资料:
- https://www.vpsee.com/2011/03/install-nginx-with-geoip-module-for-country-targeting/
- http://www.dnsdizhi.com/post-175.html
- http://blog.angryfox.com/?p=1688
我的博客即将搬运同步至腾讯云+社区,邀请大家一同入驻:https://cloud.tencent.com/developer/support-plan
- 上一篇: CentOS安装Redis、PHPredis扩展
- 下一篇: Centos7安装Mysql数据库