Introduction
Country-based redirection allows websites to redirect visitors to different domains based on their geographical location. This article explains how to configure country-based redirection in Nginx when the web server is behind a load balancer.
Prerequisites
Nginx web server should be installed and configured. The Nginx server should be behind a load balancer. GeoIP database (GeoIP.dat) should be available on the server. Root or sudo access to modify Nginx configuration files. A domain configured for country-based redirection.
Assumption:
- Nginx webserver. If nginx is not installed, follow the posts.
- Redirects India customer to example.in and rest of the country to example.com
- Webserver is behind the load balancer.
Implementation:
If you followed our previous articles then the default configuration path will be /etc/nginx/nginx.conf and domain configuration will be /etc/nginx/sites-enabled/default.conf
Open the nginx configuration and add the below lines under http section
vi /etc/nginx/nginx.conf
real_ip_header X-Forwarded-For; set_real_ip_from 0.0.0.0/0; real_ip_recursive on; geoip_country /usr/share/GeoIP/GeoIP.dat;
Please make sure that the file /usr/share/GeoIP/GeoIP.dat exists.
Where,
The X-Forwarded-For and X-Real-IP directives in the above config will properly forward the Client IP from the Nginx server.
real_ip_recursive to on will return the leftmost (original) IP instead of the rightmost (most recent) IP from the X-Forwarded-For header.
Open the site configuration file(default config is /etc/nginx/sites-enabled/default.conf) and append the below line inside the server block.
vi /etc/nginx/sites-enabled/default.conf
if ($geoip_country_code = IN){
return 301 http://example.in;
}
The above line will redirect the traffic to example.in incase if the traffic is generating from India.
Verify the nginx syntax
nginx -t
Restart the nginx service.
service nginx restart
Access the domain or IP address from India and the request will be redirected to example.in.
Conclusion
By configuring the GeoIP database and client IP forwarding in Nginx, website traffic can be redirected based on the visitor’s country. This setup helps serve country-specific domains and provides a more localized experience for users.
FAQs
1. How does country-based redirection work in Nginx?
Nginx uses the GeoIP database to identify the visitor’s country based on their IP address and redirects the request accordingly.
2. How does Nginx identify the original client IP behind a load balancer?
Nginx uses the X-Forwarded-For header along with the real_ip_header and real_ip_recursive directives to identify the original client IP.
3. What happens if the GeoIP database is missing?
Country detection will not work correctly. Make sure the GeoIP database file, such as /usr/share/GeoIP/GeoIP.dat, exists and is accessible to Nginx.
Related Articles
Enable-remote-ip-address-logging-apache2-behind-load-balancer
Talk to our experts:
Looking for the right technology solution for your business?. Our experts can help with web hosting, domain registration, DevOps and cloud, software development, web applications, mobile applications, and enterprise solutions. Get in touch with our team here.