前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >nginx使用GeoIP限制国家访问

nginx使用GeoIP限制国家访问

作者头像
行 者
发布2018-03-26 15:38:26
3.6K0
发布2018-03-26 15:38:26
举报
文章被收录于专栏:运维技术迷

安装GEeoip库

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

代码语言:javascript
复制
[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(Nginx)

代码语言:javascript
复制
[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
<pre lang="bash" line="1" escaped="true">
 
<h2><strong>配置openresty支持Geoip</strong></h2>
<pre lang="bash" line="1" escaped="true">
[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

测试访问效果

本地ip地址江苏苏州移动

《nginx使用GeoIP限制国家访问》
《nginx使用GeoIP限制国家访问》

访问提示404,和预期的效果一样。

《nginx使用GeoIP限制国家访问》
《nginx使用GeoIP限制国家访问》

参考文献: [1]:https://www.vpsee.com/2011/03/install-nginx-with-geoip-module-for-country-targeting/ [2]:http://www.dnsdizhi.com/post-175.html [3]:http://blog.angryfox.com/?p=1688

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 安装GEeoip库
  • 安装openresty(Nginx)
  • 测试访问效果
相关产品与服务
数据库
云数据库为企业提供了完善的关系型数据库、非关系型数据库、分析型数据库和数据库生态工具。您可以通过产品选择和组合搭建,轻松实现高可靠、高可用性、高性能等数据库需求。云数据库服务也可大幅减少您的运维工作量,更专注于业务发展,让企业一站式享受数据上云及分布式架构的技术红利!
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档