How to use Nginx to redirect users based on their locations

Where necessary, you may need to have access to a VPS server so you can follow how to implement the steps in this article.  You can get a cheaper VPS Server from Contabo with 4vCPU cores, 8GM RAM, and 32TB Bandwidth for less than $5.50 per month. Get this deal here now

Table of Contents

Cloud VPS S

$5.50 Monthly
  • 4 vCPU Cores | 8GB RAM


$15.50 Monthly
  • 6 vCPU Cores | 16GB RAM


$17.50 Monthly
  • 8 vCPU Cores | 24GB RAM

Nginx offers capabilities to redirect users based on their geographical locations. This can be particularly useful for various reasons such as content localization, compliance with regional regulations, or optimizing user experience. In this article, we will explore how to leverage Nginx to implement location-based redirections with practical command examples.

Before diving into the configuration, ensure that you have Nginx installed on your server. You can install it using package managers like apt or yum. Once installed, proceed with the following steps.

Creating a GeoIP Database

To redirect users based on their locations, we need a GeoIP database. You can use the MaxMind GeoIP2 database, which provides accurate information about the user’s location based on their IP address.

Copy and paste the following commands on your server terminal:

sudo apt-get install -y libmaxminddb0 libmaxminddb-dev mmdb-bin
sudo mkdir -p /usr/share/GeoIP
sudo wget -N -P /usr/share/GeoIP/
sudo gunzip /usr/share/GeoIP/GeoLite2-Country.mmdb.gz

Nginx Configuration

Now that we have the GeoIP database, let’s configure Nginx to use it for redirection.

sudo nano /etc/nginx/nginx.conf

Add the following lines to the server block to enable GeoIP module:

http {
    geoip2 /usr/share/GeoIP/GeoLite2-Country.mmdb {
        $geoip2_data_country_code country iso_code;

Implementing Redirection Rules

Now, let’s create specific redirection rules based on the user’s country code.

server {
    listen 80;

    # Redirect users from the US to a specific page
    if ($geoip2_data_country_code = US) {
        return 301;

    # Redirect users from the UK to another page
    if ($geoip2_data_country_code = GB) {
        return 301;

    # Default redirect for other countries
    return 301;

Test and Reload Nginx

After saving the changes, test the configuration and reload Nginx.

# Test Nginx configuration
sudo nginx -t

# Reload Nginx to apply changes
sudo service nginx reload

Now, users visiting your domain will be redirected based on their geographical locations.


In this tutorial, we’ve covered the essential steps to use Nginx for redirecting users based on their locations. Integrating GeoIP databases and configuring Nginx appropriately can enhance user experience, comply with regional regulations, and efficiently manage content localization.

Hire us to handle what you want

Hire us through our Fiverr Profile and leave all the complicated & technical stuff to us. Here are some of the things we can do for you:

  • Website migration, troubleshooting, and maintenance.
  • Server & application deployment, scaling, troubleshooting, and maintenance
  • Deployment of Kubernetes, Docker, Cloudron, Ant Media, Apache, Nginx,  OpenVPN, cPanel, WHMCS, WordPress, and more
  • Everything you need on AWS, IBM Cloud, GCP, Azure, Oracle Cloud, Alibaba Cloud, Linode, Contabo, DigitalOcean, Ionos, Vultr, GoDaddy, HostGator, Namecheap, DreamHost, and more.

We will design, configure, deploy, or troubleshoot anything you want. Starting from $10, we will get your job done in the shortest time possible. Your payment is safe with Fiverr as we will only be paid once your project is completed.