Welcome! Log In Create A New Profile

Advanced

Proper way to redirect on specific countries with GeoIP

Posted by vwyoda 
Proper way to redirect on specific countries with GeoIP
April 08, 2013 12:13AM
I have GeoIP working on my sites in the actual pages, I have enabled it my nginx.conf file

http {
set_real_ip_from 127.0.0.1;
real_ip_header X-Forwarded-For;
# GEO
geoip_country /etc/nginx/geoip/GeoIP.dat;
geoip_city /etc/nginx/geoip/GeoLiteCity.dat;
}

Defined in my fastcgi_params

fastcgi_param GEOIP_COUNTRY_CODE $geoip_country_code;
fastcgi_param GEOIP_COUNTRY_CODE3 $geoip_country_code3;
fastcgi_param GEOIP_COUNTRY_NAME $geoip_country_name;
fastcgi_param GEOIP_CITY_COUNTRY_CODE $geoip_city_country_code;
fastcgi_param GEOIP_CITY_COUNTRY_CODE3 $geoip_city_country_code3;
fastcgi_param GEOIP_CITY_COUNTRY_NAME $geoip_city_country_name;
fastcgi_param GEOIP_REGION $geoip_region;
fastcgi_param GEOIP_CITY $geoip_city;
fastcgi_param GEOIP_POSTAL_CODE $geoip_postal_code;
fastcgi_param GEOIP_CITY_CONTINENT_CODE $geoip_city_continent_code;
fastcgi_param GEOIP_LATITUDE $geoip_latitude;
fastcgi_param GEOIP_LONGITUDE $geoip_longitude;

What I am trying to do but can't figure out how to with NGINX is to catch any incoming hit to the server regardless of the domain or subdomain to redirect to a specific link. I have been trying what I can find online, which most point to using, if's which from my understanding should be avoided. So I am trying to sort out the actual proper method. I have Varnish on my front end

So say someone from Germany hits the server, I want NGINX to be able to catch the request and redirect to a specific page all the rest work as normal. I am thinking if I add something after my define in http in the nginx.conf file would be the best place to catch it.

Thank you for any help on this and if I need to post more about my config let me know.
Re: Proper way to redirect on specific countries with GeoIP
May 05, 2013 05:27PM
Use nginx map module: http://nginx.org/en/docs/http/ngx_http_map_module.html#map

The below example will work only when accessing the root location, e.g. http://yoursite/


http {
....
map $geoip_country_code $new_home_uri {
default /en/;
uk /en/;
us /en/;
de /de/;
ru /ru/;
}
....
}


server {

....

location = / {
rewrite ^ $new_home_uri break;
}

location = /de {
# default home for german visitors
...
}

location = /ru {
# default home for russian visitors
...
}

location = /en {
# default home for american, british and any other visitors
...
}

}
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 84
Record Number of Users: 6 on February 13, 2018
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready