2

Can't find solution how to solve this. Here is how I blocked an access to the country and at the same time I need to grand access to a specific IP that is from blocked country.

2 Answers 2

8

Finally found the solution for this problem.

1) in nginx.conf add

http {

    geoip_country /usr/share/GeoIP/GeoIP.dat;

    map $geoip_country_code $allowed_country {
        default no;
        LV yes; # in my case it is Latvia (allowed country, but all other are not)
    }

    geo $exclusions {

        default 0;

        123.123.123.123 1;  # here comes allowed IP that is in blocked country list

    }

}

2) in your vhost configuration server{} container

if ($allowed_country = yes) {
    set $exclusions 1;
}


if ($exclusions = "0") {
    return 403;
}

The main idea is from HERE

3

In http block

geoip_country /usr/local/share/GeoIP/GeoIP.dat;

map "$geoip_country_code:$remote_addr" $allowed_country {
    default yes;
    "~..:10.11.12.13" yes;
    "~..:11.12.13.14" no;
    "~TR:.*" no;
}

In server block

if ($allowed_country = no) {
    return 403;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.