# GeoIP country blocking - whitelist specific IPs

**URL:** <https://www.ruby-forum.com/t/geoip-country-blocking-whitelist-specific-ips/224458>\
**Category:** NGINX\
**Created:** [November 12, 2012, 9:03pm UTC](https://www.ruby-forum.com/t/geoip-country-blocking-whitelist-specific-ips/224458 "2012-11-12T21:03:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![aris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@aris](https://www.ruby-forum.com/u/aris)\
**Post date:** [November 12, 2012, 9:03pm UTC](https://www.ruby-forum.com/t/geoip-country-blocking-whitelist-specific-ips/224458/1 "2012-11-12T21:03:41Z")

</div>

Hello NGINX users,

I’m facing a little issue with country bans over GeoIP. I’m using the  
following code within my server directive:

if ($geoip\_country\_code ~ (BR|CN|KR|RU) ) {  
return 123;  
}

123 returns an error page informing the visitor that his country is  
blocked.  
Now let’s say I’ve got some visitors from russia, who should still be  
able  
to access my website. How would I archive this? Of course something like  
“allow 1.2.3.4;” doesn’t work with the code above. Any suggestions?

Thanks in advance!

Posted at Nginx Forum:

> **[GeoIP country blocking - whitelist specific IPs](https://forum.nginx.org/read.php?2,232745,232745#msg-232745)**
>
> Hello NGINX users, I'm facing a little issue with country bans over GeoIP. I'm using the following code within my server directive: if ($geoip\_country\_code ~ (BR|CN|KR|RU) ) { return 123; } 123 returns an error page informing the visitor tha

---

<div class="post-metadata">

**Author:** ![Infinitnet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/9d8465/32.png) [@Infinitnet](https://www.ruby-forum.com/u/Infinitnet)\
**Post date:** [November 13, 2012, 12:03pm UTC](https://www.ruby-forum.com/t/geoip-country-blocking-whitelist-specific-ips/224458/2 "2012-11-13T12:03:10Z")

</div>

Hi

We use something similar to this:

geoip\_country /path/to/GeoIP.dat;

geo $allowed\_ranges {  
default 0;  
1.2.3.0/24 1;  
10.0.0.0/8 1;  
127.0.0.1 1;  
}

map $geoip\_country\_code $blocked\_country {  
default 1;  
A2 0; # Satellite Provider  
O1 0; # Other Country  
AD 1; # Andorra  
AP 1; # Asia/Pacific Region  
AQ 1; # Antarctica  
}

if ($blocked\_country) {  
set $deny\_request 1;  
}

if ($allowed\_ranges) {  
set $deny\_request 0;  
}

if ($deny\_request) {

# Do whatever you want to do here …

}

Hope that it makes sense? 🙂

L.

---

<div class="post-metadata">

**Author:** ![Infinitnet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/9d8465/32.png) [@Infinitnet](https://www.ruby-forum.com/u/Infinitnet)\
**Post date:** [November 13, 2012, 12:21pm UTC](https://www.ruby-forum.com/t/geoip-country-blocking-whitelist-specific-ips/224458/3 "2012-11-13T12:21:53Z")

</div>

Hello,

thanks for your reply! Your solution does indeed make sense and I’ve  
been  
using something similar before. Just thought there might be something  
that  
wouldn’t require rewriting my current syntax, such as:

if ($geoip\_country\_code ~ (BR|CN|KR|RU) ) {  
if ($remote\_addr = (1.2.3.4|1.2.3.5|1.2.3.6) ) {  
break;  
}  
return 123;  
}

Posted at Nginx Forum:

> **[Re: GeoIP country blocking - whitelist specific IPs](https://forum.nginx.org/read.php?2,232745,232762#msg-232762)**
>
> Hello NGINX users, I'm facing a little issue with country bans over GeoIP. I'm using the following code within my server directive: if ($geoip\_country\_code ~ (BR|CN|KR|RU) ) { return 123; } 123 returns an error page informing the visitor tha

---

<div class="post-metadata">

**Author:** ![Infinitnet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/9d8465/32.png) [@Infinitnet](https://www.ruby-forum.com/u/Infinitnet)\
**Post date:** [November 13, 2012, 12:24pm UTC](https://www.ruby-forum.com/t/geoip-country-blocking-whitelist-specific-ips/224458/4 "2012-11-13T12:24:13Z")

</div>

…or some elif function, but you get the idea.

Posted at Nginx Forum:

> **[Re: GeoIP country blocking - whitelist specific IPs](https://forum.nginx.org/read.php?2,232745,232764#msg-232764)**
>
> Hello NGINX users, I'm facing a little issue with country bans over GeoIP. I'm using the following code within my server directive: if ($geoip\_country\_code ~ (BR|CN|KR|RU) ) { return 123; } 123 returns an error page informing the visitor tha
