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 的配置異常,重啟使用時測試正常


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM