Nginx 配置GeoIP2 禁止访问,并允许添加白名单过滤访问设置

配置环境:Centos 7.6 + Tengine 2.3.2

GeoIP2 下载地址:https://dev.maxmind.com/geoip/geoip2/geolite2/

1. Nginx  HTTP 块配置,此区块只做配置列表,并无限制条约,置如下

geoip2 /usr/local/nginx/GeoIP/GeoLite2-Country.mmdb {
                $geoip2_data_country_code country iso_code;
        }
 #允许本地网段访问   
        geo $allow-ip{
                default no;
                192.168.10.0/24
        }
#配置地区访问
        map $geoip2_data_country_code $allowed_country {
                default no;
                CN yes;
        }
http{

 

 

 

2.  server 块主要启用上属所定义的配置限制要求,配置如下;
   此配置在location 前面,也可以配置在localtion 里面,经测试如自定义403 404 异常返回页面,将放置到localtion 里面自定义页面才可生效;

#自定义403 404 返回页面
error_page 403 404 /403.html;
    location = /403.html {
            root  /var/www/;
    }

    location / {
         #本地网段白名单
            if ($allow-ip = yes ) {
                    set $allowed_country yes;
            }
         #国家地区白名单
            if ($allowed_country = no) {
                    return 403;
            }


            proxy_pass http://huangting_web;
            include conf.d/proxy.conf;
    }
View Code

 

 

 

3. 备注:个人所在配置时启用SSL , 配置文件与nginx.conf 使用include 外部调用,所有在nginx -t 测试提示ssl on 的配置异常,重启使用时测试正常

posted @ 2019-11-25 12:32  Yee.Liu  阅读(1284)  评论(0编辑  收藏  举报