When for any reason you can’t or don’t want to use GeoIP module in Magento, here’s how you can filter IPs by location on Nginx level

Install Nginx GeoIP module and GeoIP database as described here. Make sure you are handling “exotic” cases like IP’s from VPN’s and satellite providers.

nginx.conf
map $geoip_country_code $area {
    default world;
# These codes are returned in case of proxies and exotic cases: http://dev.maxmind.com/geoip/legacy/mod_geoip2/
    A1 eea; # anonymous proxies
    A2 eea; # satellite provider
    EU eea; # an IP in a block used by multiple European countries
    AP eea; # an IP in a block used by multiple Asia/Pacific region countries
    O1 eea; # "Other Country"
    US us; # US visitors will have their own rule
# The list of EU countries
    AT eea;
    BE eea;
    BG eea;
    HR eea;
    CY eea;
    CZ eea;
    DK eea;
    EE eea;
    FI eea;
    FR eea;
    DE eea;
    GR eea;
    HU eea;
    IE eea;
    IT eea;
    LV eea;
    LT eea;
    LU eea;
    MT eea;
    NL eea;
    PL eea;
    PT eea;
    RO eea;
    SK eea;
    SI eea;
    ES eea;
    SE eea;
    GB eea;
    IS eea;
    LI eea;
    NO eea;
    CH eea;
}

MaxMind GeoIP database never “fails” IP lookup. If it is impossible to return country code, it will return “01” country code. According to the docs, this works only if you are using GeoLiteCity.dat database (geoip_city setting in Nginx).

It is best to have separate domains (and therefore separate server blocks) for each geo-based redirect. At least, have some subfolders like www.example.com/uk so that you could split rewrites by location blocks.

Having all rules in one server and one location block will lead to endless redirect loops and you will end up with some magic in your ghost configuration:

set $redirect "0";
if ($area = "world") { set $redirect "go"; }
if ($request_uri ~* "___store") { set $redirect ""; }
if ($request_method = POST) { set $redirect ""; }
if ($http_user_agent ~* "Google|Bing|Yandex") { set $redirect ""; }
if ($arg_bypass = "yes")                      { set $redirect ""; }
if ($request_uri ~* "/js/")   { set $redirect ""; }
if ($request_uri ~* "/media/")   { set $redirect ""; }
if ($request_uri ~* "/skin/")   { set $redirect ""; }
if ($request_uri ~* "/(index.php/)?admin/")   { set $redirect ""; }
if ($request_uri ~* "/(index.php/)?adminhtml_profile/")   { set $redirect ""; }
if ($request_uri ~* "/(index.php/)?urapidflowadmin/")   { set $redirect ""; }
if ($request_uri ~* "/(index.php/)?amshopby/")   { set $redirect ""; }
if ($request_uri ~* "/(index.php/)?amimgupload/")   { set $redirect ""; }
if ($request_uri ~* "/(index.php/)?ampgrid/")   { set $redirect ""; }
if ($redirect = "go") { return 301 https://www.divestock.com$1?___store=int; }
if ($area = "us" ) { return 301 https://www.divestock.us$request_uri ;}

Leave a Reply

© 2017 Accolade Ltd. All Rights Reserved.