GeoDNS and Geo-Routing Configuration
GeoDNS directs users to the nearest server at DNS resolution level, before TCP connection. This reduces latency and allows splitting traffic by geographic zones.
GeoDNS Providers
Cloudflare Load Balancing — geo-steering out of the box:
# Cloudflare API: create Load Balancer
curl -X POST "https://api.cloudflare.com/client/v4/zones/{zone_id}/load_balancers" \
-H "Authorization: Bearer {token}" \
-H "Content-Type: application/json" \
--data '{
"name": "api.mysite.com",
"fallback_pool": "us-east-1-pool",
"default_pools": ["us-east-1-pool"],
"region_pools": {
"ENAM": ["us-east-1-pool"],
"WNAM": ["us-west-2-pool"],
"EEU": ["eu-central-1-pool"],
"WEU": ["eu-west-1-pool"],
"SEAS": ["ap-southeast-1-pool"],
"NEAS": ["ap-northeast-1-pool"]
},
"steering_policy": "geo",
"session_affinity": "ip_cookie",
"session_affinity_ttl": 300
}'
AWS Route 53 Geolocation Routing:
# Terraform
resource "aws_route53_record" "api_eu" {
zone_id = var.zone_id
name = "api.mysite.com"
type = "A"
set_identifier = "eu-users"
geolocation_routing_policy {
continent = "EU"
}
alias {
name = aws_lb.eu_west_1.dns_name
zone_id = aws_lb.eu_west_1.zone_id
evaluate_target_health = true
}
}
resource "aws_route53_record" "api_default" {
zone_id = var.zone_id
name = "api.mysite.com"
type = "A"
set_identifier = "default"
geolocation_routing_policy {
country = "*" # Default: all other regions
}
alias {
name = aws_lb.us_east_1.dns_name
zone_id = aws_lb.us_east_1.zone_id
evaluate_target_health = true
}
}
AWS Route 53 Latency-based Routing (recommended over geolocation):
# Route traffic not by geography, but by real latency
resource "aws_route53_record" "api_latency_eu" {
zone_id = var.zone_id
name = "api.mysite.com"
type = "A"
set_identifier = "eu-west-1"
latency_routing_policy {
region = "eu-west-1"
}
alias {
name = aws_lb.eu.dns_name
zone_id = aws_lb.eu.zone_id
evaluate_target_health = true
}
}
Nginx: Geo-Routing at Application Level
For fine-tuning — MaxMind GeoIP2:
# Install module
apt install nginx-module-geoip2 libmaxminddb-dev
# Download GeoLite2 database
mkdir -p /etc/nginx/geoip
cd /etc/nginx/geoip
curl -L "https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-Country&license_key={KEY}&suffix=tar.gz" \
| tar xz --strip-components=1
# nginx.conf
load_module modules/ngx_http_geoip2_module.so;
http {
geoip2 /etc/nginx/geoip/GeoLite2-Country.mmdb {
$geoip2_country_code country iso_code;
$geoip2_country_name country names en;
}
# Map countries to backends
map $geoip2_country_code $backend {
default http://us-backend:3000;
RU http://ru-backend:3000;
UA http://eu-backend:3000;
"~^(DE|AT|CH)" http://eu-backend:3000;
"~^(GB|IE|FR)" http://eu-backend:3000;
}
# Map for language version redirect
map $geoip2_country_code $lang_prefix {
default /en;
RU /ru;
UA /ua;
DE /de;
}
server {
listen 80;
location / {
# Auto redirect by geo (only first visit)
if ($cookie_lang_set = "") {
return 302 $lang_prefix$request_uri;
}
proxy_pass $backend;
proxy_set_header X-Country-Code $geoip2_country_code;
proxy_set_header X-Real-IP $remote_addr;
}
}
}
GeoDNS Testing
# Check from different resolvers (simulating different regions)
dig @8.8.8.8 api.mysite.com # Google DNS (US)
dig @1.1.1.1 api.mysite.com # Cloudflare (EU)
dig @77.88.8.8 api.mysite.com # Yandex DNS (RU)
# Via curl with geolocation simulation (VPN or proxy)
curl -v https://api.mysite.com/health
# Cloudflare trace
curl https://mysite.com/cdn-cgi/trace | grep colo
# colo=AMS → Amsterdam PoP
# Check IP geolocation
curl https://ipapi.co/{IP}/json/ | jq '.country_code, .region'
DNS TTL and Caching
With GeoDNS proper TTL setup is important:
TTL = 30s → fast failover, high NS load
TTL = 300s → good balance (recommended)
TTL = 3600s → reduces DNS load, slow failover
Cloudflare Load Balancing automatically manages TTL on failover.
GeoDNS setup via Cloudflare with two regions — 1–2 working days. Via AWS Route 53 with Terraform — 2–3 days.







