diff --git a/nginx/templates/01_geoip2.conf.template b/nginx/templates/01_geoip2.conf.template new file mode 100644 index 0000000..c3e9456 --- /dev/null +++ b/nginx/templates/01_geoip2.conf.template @@ -0,0 +1,42 @@ +# --------------------------------------------------------- +# GeoIP2 +# --------------------------------------------------------- + +# База MaxMind GeoLite2 Country +# файл должен быть заранее скачан и примонтирован +geoip2 /etc/angie/geoip/GeoLite2-Country.mmdb { + auto_reload 5m; + $geoip2_country_code source=$geoip_source_ip country iso_code; + $geoip2_country_name source=$http_x_forwarded_for country names ru; +} + + +# если есть X-Debug-IP — используем его, иначе реальный адрес +map $http_x_debug_ip $geoip_source_ip { + default $remote_addr; + ~^\d+\.\d+\.\d+\.\d+$ $http_x_debug_ip; +} + + +# --------------------------------------------------------- +# Country map +# --------------------------------------------------------- +map $geoip2_country_code $is_ru { + default 0; + RU 1; +} + +log_format geoip_debug + 'real_ip=$remote_addr ' + 'geoip_ip=$geoip_source_ip ' + 'country=$geoip2_country_code ' + 'is_ru=$is_ru ' + 'status=$status'; + +# --------------------------------------------------------- +# Geo-based origin selection +# --------------------------------------------------------- +map $is_ru $target_origin { + 1 ru.${ ORIGIN_NAME }; + default ${ ORIGIN_NAME }; +} \ No newline at end of file diff --git a/nginx/templates/edge-https-proxy.conf.template b/nginx/templates/edge-https-proxy.conf.template index c8d495e..261548c 100644 --- a/nginx/templates/edge-https-proxy.conf.template +++ b/nginx/templates/edge-https-proxy.conf.template @@ -25,7 +25,7 @@ server { # --------------------------------------------------------- server { listen 443 ssl; - server_name *.${ORIGIN_NAME}; + server_name *.${ ORIGIN_NAME }; # SSL сертификаты (скопированы с origin / CF origin cert) ssl_certificate /etc/nginx/certs/fullchain.pem; @@ -40,6 +40,14 @@ server { ssl_session_cache shared:SSL:10m; ssl_session_timeout 1h; + # ----------------------------------------------------- + # Geo redirect for non-RU traffic + # ----------------------------------------------------- + # if используется ТОЛЬКО для return — это безопасно + if ($is_ru = 0) { + return 302 https://${ ORIGIN_NAME }$request_uri; + } + # ----------------------------------------------------- # Proxy # -----------------------------------------------------