Nginx+GeoIP2进行地域限制

用nginx的geoip/geoip2模块来限制地区的访问;

1、安装相应地区地址模块库相应的包

yum install libmaxminddb libmaxminddb-devel -y

数据库地址:

http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.mmdb.gz

http://geolite.maxmind.com/download/geoip/database/GeoLite2-Country.mmdb.gz

我这是放在 /usr/local/share/GeoIP/ 里面。

2、检查nginx是否已经安装ngx_http_geoip2_module模块

nginx -V

3、如果没有安装,重新编译安装nginx的ngx_http_geoip2_module

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

tar -xf nginx-1.10.1.tar.gz 

cd nginx-1.10.1

./configure --prefix=/home/deploy/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module  --add-dynamic-module=/home/deploy/nginx/ngx_http_geoip2_module

make && make install

4、nginx配置文件规则设置

http段进行国家代码的map映射:

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

    $geoip2_data_country_code country iso_code;

  }

  map $geoip2_data_country_code $allowed_country {

    default no;

    CN yes;

  }

站点的server段里面加一下拒绝:

if ($allowed_country = no) {

      return 403;

    }

最后重启一下nginx就行了。

我这的规则是默认拒绝,只允许中国,可以根据情况调整。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Nginx中自带GeoIP模块可以屏蔽指定IP的请求,只不过默认没有被编译,打开以后我们只要再下载一个IP规则就行...
    ebayboy阅读 9,226评论 0 0
  • 获取全套nginx教程,请访问瓦力博客 ngx_http_geoip_module是对IP地址进行地域信息的读取。...
    瓦力博客阅读 2,717评论 0 4
  • nginx 编译参数详解 标题是不是很欠揍,个人认为确实值得一看,如果你不了解nginx,或者你刚学nginx,或...
    可不可以_78b2阅读 240评论 0 0
  • 常用文档连接: 内置变量:http://nginx.org/en/docs/varindex.html内置指令:h...
    西伯利亚狼_阅读 494评论 0 0
  • 1.什么是负载均衡?通过某种负载分担技术,将外部发送来的请求按照某种策略分配到服务器集合的某一台服务器上,而接收到...
    少艾_91df阅读 540评论 0 0