NGINX Geo Routing Explained

Learn how to configure Geo Routing in NGINX to route users based on country and region using GeoIP2. Includes practical examples, backend mapping, proxy_pass setup, and real-world production use cases.

May 22, 2026 - 14:28
Updated: 9 hours ago
1 5
NGINX Geo Routing Explained

Geo Routing in NGINX — Serve Users From the Right Region 🌍

Modern applications are global.

Users access applications from different countries and regions.
Serving all traffic from one backend increases latency and affects performance.

Using NGINX Geo Routing, you can route users to the nearest backend server based on their location.

What is Geo Routing?

Geo routing means sending users to different backend servers depending on their country or region.

Example:

  • India users → Mumbai backend
  • Europe users → Frankfurt backend
  • US users → Virginia backend

Benefits:

  • Lower latency
  • Better performance
  • Improved scalability
  • Better user experience

Install GeoIP Module

sudo apt update

sudo apt install libnginx-mod-http-geoip2 mmdb-bin geoipupdate

Create GeoIP Directory

sudo mkdir -p /etc/nginx/geoip
cd /etc/nginx/geoip

Download the GeoLite2 Country database from MaxMind.

Configure GeoIP in NGINX

Edit:

sudo nano /etc/nginx/nginx.conf

Add inside the http block:

http {

    geoip2 /etc/nginx/geoip/GeoLite2-Country.mmdb {
        auto_reload 5m;

        $geoip2_data_country_code country iso_code;
    }

}

This variable will store the country code:

$geoip2_data_country_code

Examples:

  • IN
  • US
  • DE
  • SG

Configure Regional Backends

upstream india_backend {
    server 10.0.1.10:80;
}

upstream us_backend {
    server 10.0.2.10:80;
}

upstream eu_backend {
    server 10.0.3.10:80;
}

Map Country to Backend

map $geoip2_data_country_code $backend {

    default         us_backend;

    IN              india_backend;

    US              us_backend;

    DE              eu_backend;

    FR              eu_backend;
}

Configure Server Block

server {

    listen 80;

    server_name example.com;

    location / {

        proxy_pass http://$backend;

        proxy_set_header Host $host;

        proxy_set_header X-Real-IP $remote_addr;

        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

Example Routing

India User:
Country Code → IN
Backend → india_backend

Germany User:
Country Code → DE
Backend → eu_backend

Unknown Country:
Fallback → us_backend

Advanced Redirect Example

if ($geoip2_data_country_code = IN) {
    return 302 https://in.example.com;
}

if ($geoip2_data_country_code = US) {
    return 302 https://us.example.com;
}

Enable Country Logging

log_format geo '$remote_addr - $geoip2_data_country_code';

access_log /var/log/nginx/geo.log geo;

Test NGINX Configuration

sudo nginx -t

Reload NGINX:

sudo systemctl reload nginx

Real World Use Cases

  • Multi-region applications
  • CDN origin routing
  • Kubernetes traffic routing
  • Disaster recovery
  • Region-specific APIs
  • GDPR traffic isolation

Final Thoughts

NGINX is not just a reverse proxy.

It can also work as:

  • Geo-aware traffic router
  • Global load balancer
  • Edge gateway
  • Multi-region traffic controller

Geo routing helps improve:

  • Application performance
  • Scalability
  • User experience
  • Traffic optimization

Small NGINX configurations can make a huge difference at scale.

What's Your Reaction?

Like Like 2
Dislike Dislike 0
Love Love 0
Funny Funny 0
Wow Wow 0
Sad Sad 0
Angry Angry 0

Comments (1)

User
Sj
Sj 49 minutes ago
Nice