Question

NGINX GeoIP Redirect and Search Engine Bots

I have managed to get NGINX GEOIP by Country to redirect, the following is what I have done:

nginx.conf

http {

        ##
        # Add GeoIP to NGINX
        ##
        geoip_city /usr/share/GeoIP/GeoLiteCity.dat;
        geoip_country /usr/share/GeoIP/GeoIP.dat;
...
}

in my host file I placed the following

map $geoip_city_country_code $closest_server {
    default example.com;
    NZ      nz.example.com;
    CA      ca.example.com;
    US      us.example.com;
    GB      gb.example.com;
}

server {
...
server_name example.com nz.example.com ca.example.com us.example.com gb.example.com;
...
if ($closest_server != $host) {
    rewrite ^ $scheme://$closest_server$request_uri break;
}

What I need it to do is to redirect to the closest server but also allow the user to select another international subdomain and not redirect.

Secondly, I need to exclude search engine bots to be excluded from these rules.

Any assistance would be great.

Subscribe
Share

Submit an answer
You can type!ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

@bris4171 I found a solution, how to allow the user to select another international subdomain and not redirect - with http referer.

map $http_cf_ipcountry $regional_server {
    default            "en.example.org";
    "US"               "example.org";
}


   if ($regional_server != $host) {
      set $lang A;
   }

   if ($http_referer !~* "example.") {
      set $lang "${lang}B";
   }

   if ($lang = AB) {
      rewrite ^ $scheme://$regional_server$request_uri break;
   }

$http_cf_ipcountry - I use cloudflare geoip solution

Now users can change subdomain

@bris4171 did you solve this? I have the same issue, how to allow the user to select another international subdomain and not redirect.