首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >nginx使用GeoIP限制国家访问

nginx使用GeoIP限制国家访问

作者头像
行 者
发布2018-03-26 15:38:26
发布2018-03-26 15:38:26
3.7K00
代码可运行
举报
文章被收录于专栏:运维技术迷运维技术迷
运行总次数:0
代码可运行

安装GEeoip库

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

代码语言:javascript
代码运行次数:0
运行
复制
[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
代码运行次数:0
运行
复制
[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地址江苏苏州移动

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

参考文献: [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 归档