{"id":2246,"date":"2017-12-19T20:34:13","date_gmt":"2017-12-19T15:04:13","guid":{"rendered":"https:\/\/pheonixsolutions.com\/blog\/?p=2246"},"modified":"2026-09-11T12:29:42","modified_gmt":"2026-09-11T06:59:42","slug":"country-based-redirection-nginx-behind-load-balancergeo-location-based-redirection","status":"publish","type":"post","link":"https:\/\/pheonixsolutions.com\/blog\/country-based-redirection-nginx-behind-load-balancergeo-location-based-redirection\/","title":{"rendered":"How to Configure Country-Based Redirection in Nginx Behind a Load Balancer"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Introduction<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Nginx web server should be installed and configured. The Nginx server should be behind a load balancer. GeoIP database (<code>GeoIP.dat<\/code>) should be available on the server. Root or sudo access to modify Nginx configuration files. A domain configured for country-based redirection.<\/p>\n\n\n<h4>Assumption:<\/h4>\n<ol>\n<li>Nginx webserver. If nginx is not installed, follow the posts.\n<ol>\n<li><a href=\"https:\/\/pheonixsolutions.com\/blog\/install-nginx-php-mariadb-ubuntu-16-04-1\/\">Ubuntu 16.04<\/a><\/li>\n<li><a href=\"https:\/\/pheonixsolutions.com\/blog\/install-nginx-php-fpm-mariadb-on-centos-7\/\">Centos 7<\/a><\/li>\n<\/ol>\n<\/li>\n<li>Redirects India customer to\u00a0<em>example.in\u00a0<\/em>and rest of the country to\u00a0<em>example.com<\/em><\/li>\n<li>Webserver is behind the load balancer.<\/li>\n<\/ol>\n<h4>Implementation:<\/h4>\n<p>If you followed our previous articles then the default configuration path will be\u00a0<em>\/etc\/nginx\/nginx.conf\u00a0<\/em>and domain configuration will be\u00a0<em>\/etc\/nginx\/sites-enabled\/default.conf<\/em><\/p>\n<p>Open the nginx configuration and add the below lines under\u00a0<em>http\u00a0<\/em>section<\/p>\n<p><code>vi \/etc\/nginx\/nginx.conf<\/code><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">real_ip_header X-Forwarded-For;\nset_real_ip_from 0.0.0.0\/0;\nreal_ip_recursive on;\ngeoip_country \/usr\/share\/GeoIP\/GeoIP.dat;\n<\/pre>\n<p>Please make sure that the file \/usr\/share\/GeoIP\/GeoIP.dat exists.<\/p>\n<p>Where,<\/p>\n<p>The X-Forwarded-For and X-Real-IP directives in the above config will properly forward the Client IP from the Nginx server.<\/p>\n<p>real_ip_recursive to on will return the leftmost (original) IP instead of the rightmost (most recent) IP from the X-Forwarded-For header.<\/p>\n<p>Open the site configuration file(default config is\u00a0<em>\/etc\/nginx\/sites-enabled\/default.conf)\u00a0<\/em>and append the below line inside the\u00a0<em>server\u00a0<\/em> block.<\/p>\n<p><code>vi \/etc\/nginx\/sites-enabled\/default.conf<\/code><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">if ($geoip_country_code = IN){\nreturn 301 http:\/\/example.in;\n}\n<\/pre>\n<p>The above line will redirect the traffic to example.in incase if the traffic is generating from India.<\/p>\n<p>Verify the nginx syntax<\/p>\n<p><code>nginx -t<\/code><\/p>\n<p>Restart the nginx service.<\/p>\n<p><code>service nginx restart<\/code><\/p>\n<p>Access the domain or IP address from India and the request will be redirected to example.in.<\/p>\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">By configuring the GeoIP database and client IP forwarding in Nginx, website traffic can be redirected based on the visitor&#8217;s country. This setup helps serve country-specific domains and provides a more localized experience for users.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">FAQs<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>1. How does country-based redirection work in Nginx?<\/strong><br>Nginx uses the GeoIP database to identify the visitor&#8217;s country based on their IP address and redirects the request accordingly.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>2. How does Nginx identify the original client IP behind a load balancer?<\/strong><br>Nginx uses the <code>X-Forwarded-For<\/code> header along with the <code>real_ip_header<\/code> and <code>real_ip_recursive<\/code> directives to identify the original client IP.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">3<strong>. What happens if the GeoIP database is missing?<\/strong><br>Country detection will not work correctly. Make sure the GeoIP database file, such as <code>\/usr\/share\/GeoIP\/GeoIP.dat<\/code>, exists and is accessible to Nginx.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Related Articles<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/pheonixsolutions.com\/blog\/enable-remote-ip-address-logging-apache2-behind-load-balancer\/\">Enable-remote-ip-address-logging-apache2-behind-load-balancer<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Talk to our experts<strong>:<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Looking for the right technology solution for your business?. Our experts can help with\u00a0web hosting, domain registration, DevOps and cloud, software development, web applications, mobile applications, and enterprise solutions. Get in touch with our team\u00a0<a href=\"https:\/\/pheonixsolutions.com\/contact\">here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction Country-based redirection allows websites to redirect visitors to different domains based on their geographical location. This article explains how [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1022],"tags":[401,271],"class_list":["post-2246","post","type-post","status-publish","format-standard","hentry","category-web-architecture","tag-countrybasedredirection","tag-nginx","psol-cat-web-architecture"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/phn2x7-Ae","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/2246","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=2246"}],"version-history":[{"count":2,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/2246\/revisions"}],"predecessor-version":[{"id":11663,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/2246\/revisions\/11663"}],"wp:attachment":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=2246"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=2246"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=2246"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}