# Geoip filtering not working

**URL:** <https://www.ruby-forum.com/t/geoip-filtering-not-working/235755>\
**Category:** NGINX\
**Created:** [July 13, 2013, 4:09am UTC](https://www.ruby-forum.com/t/geoip-filtering-not-working/235755 "2013-07-13T04:09:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![addis\_a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c4cdca/32.png) [@addis\_a](https://www.ruby-forum.com/u/addis_a)\
**Post date:** [July 13, 2013, 4:09am UTC](https://www.ruby-forum.com/t/geoip-filtering-not-working/235755/1 "2013-07-13T04:09:50Z")

</div>

Hello,

I’m trying to figure out why Nginx’s geoip modules doesn’t seem to  
filter  
out certain ip’s from a banned country. I manually tested the GeoIP.dat  
using ‘geoiplookup’ against a few ips that successfully connected but  
were  
in a banned country. GeoIP.dat was fine, it wasn’t the problem. Maybe  
its a  
config problem? Wondering if you guys can help.

i have the following lines in config:

geoip\_country /usr/share/GeoIP/GeoIP.dat;

server {  
…  
if ($geoip\_country\_code = CN) {  
return 444;  
}  
…  
}

Please help!

Posted at Nginx Forum:

> **[geoip filtering not working](https://forum.nginx.org/read.php?2,240802,240802#msg-240802)**
>
> Hello, I'm trying to figure out why Nginx's geoip modules doesn't seem to filter out certain ip's from a banned country. I manually tested the GeoIP.dat using 'geoiplookup' against a few ips that successfully connected but were in a banned country....

---

<div class="post-metadata">

**Author:** ![bignginxfan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/7c8e57/32.png) [@bignginxfan](https://www.ruby-forum.com/u/bignginxfan)\
**Post date:** [August 16, 2013, 12:28pm UTC](https://www.ruby-forum.com/t/geoip-filtering-not-working/235755/2 "2013-08-16T12:28:08Z")

</div>

Hi, I was looking for the zip code from a given IP address few months  
back  
and now I’ve got the solution from IP2Location. You can try IP2Location  
module see if it helps in your issues and good luck.  
([IP2Location IP Geolocation Nginx Module | IP2Location](http://ip2location.com/developers/nginx))

Posted at Nginx Forum:

> **[Re: geoip filtering not working](https://forum.nginx.org/read.php?2,240802,241917#msg-241917)**
>
> Hello, I'm trying to figure out why Nginx's geoip modules doesn't seem to filter out certain ip's from a banned country. I manually tested the GeoIP.dat using 'geoiplookup' against a few ips that successfully connected but were in a banned country....

---

<div class="post-metadata">

**Author:** ![bignginxfan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/7c8e57/32.png) [@bignginxfan](https://www.ruby-forum.com/u/bignginxfan)\
**Post date:** [August 16, 2013, 3:15pm UTC](https://www.ruby-forum.com/t/geoip-filtering-not-working/235755/3 "2013-08-16T15:15:07Z")

</div>

I have this working pretty well (ok, I think _very\_well_ ) with GeoIP.

I used a MAP in the main nginx.conf like this:  
map $geoip\_country\_code $allowed\_country {  
default 0;

```
     US 1;
     GB 1;
     CA 1;
     EU 1;
 }

```

Then, in my default.conf, the first statement(s) in the relevant  
“location’s” is:

if ($allowed\_country = 0) {  
return 418;  
}

Again, this works for me, and you can add “allowed countries” in just  
one place: the “map”.

-AJ
