# Geoip redirect but exclude a specific directory

**URL:** <https://www.ruby-forum.com/t/geoip-redirect-but-exclude-a-specific-directory/199497>\
**Category:** NGINX\
**Created:** [November 22, 2010, 9:35pm UTC](https://www.ruby-forum.com/t/geoip-redirect-but-exclude-a-specific-directory/199497 "2010-11-22T21:35:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dobai-Pataky\_BSSSSl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/77aa72/32.png) [@Dobai-Pataky\_BSSSSl](https://www.ruby-forum.com/u/Dobai-Pataky_BSSSSl)\
**Post date:** [November 22, 2010, 9:35pm UTC](https://www.ruby-forum.com/t/geoip-redirect-but-exclude-a-specific-directory/199497/1 "2010-11-22T21:35:29Z")

</div>

Hello,

I`m using the method described in here to redirect people with specific  
ips to locations

```auto
http {
keepalive_timeout 65;
geoip_country /usr/local/etc/geo/GeoIP.dat ;

# these are the GeoIP exceptions for your site
geo $mygeo {
default 0;
bla/19 1;
blu/16 1;
}

then, in the server block:

location / {
include proxy-lcb-lt.conf;
if ($geoip_country_code = "US") {
proxy_pass http://prod;
}
# these are the exceptions, defined in nginx.conf:
if ($mygeo = "1") {
proxy_pass http://prod;
}
}
```

Problem is the administrator of the site is from one of the countries  
that gets redirected so he cant access the administration section of the  
site instead he gets redirected to another place.

How can i exclude the directory “admin” from the above geoip redirection  
?

Thanks for any suggestion.

Posted at Nginx Forum:

> **[Geoip redirect but exclude a specific directory](https://forum.nginx.org/read.php?2,152469,152469#msg-152469)**
>
> Hello, I\`m using the method described in here to redirect people with specific ips to locations \[code\] http { keepalive\_timeout 65; geoip\_country /usr/local/etc/geo/GeoIP.dat ; # these are the GeoIP exceptions for your site geo

---

<div class="post-metadata">

**Author:** ![st1905](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/35a633/32.png) [@st1905](https://www.ruby-forum.com/u/st1905)\
**Post date:** [November 22, 2010, 10:49pm UTC](https://www.ruby-forum.com/t/geoip-redirect-but-exclude-a-specific-directory/199497/2 "2010-11-22T22:49:03Z")

</div>

Hello!

On Mon, Nov 22, 2010 at 03:34:27PM -0500, st1905 wrote:

[…]

> Problem is the administrator of the site is from one of the countries  
> that gets redirected so he cant access the administration section of the  
> site instead he gets redirected to another place.
> 
> How can i exclude the directory “admin” from the above geoip redirection  
> ?

location / {  
…do whatever you want here…  
}

location /admin/ {  
…do whatever you want here…  
}

See here for more details:

> **[GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki...](https://github.com/nginxinc/nginx-wiki)**
>
> ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com

Maxim D.

---

<div class="post-metadata">

**Author:** ![st1905](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/35a633/32.png) [@st1905](https://www.ruby-forum.com/u/st1905)\
**Post date:** [November 23, 2010, 6:28pm UTC](https://www.ruby-forum.com/t/geoip-redirect-but-exclude-a-specific-directory/199497/3 "2010-11-23T18:28:40Z")

</div>

Tried

location ~ ^/admin/.+.php$ {

fastcgi\_pass 127.0.0.1:9000;  
root /var/www/vhosts/domain.com/httpdocs;  
fastcgi\_param SCRIPT\_FILENAME $document\_root$fastcgi\_script\_name;  
fastcgi\_index index.php;  
include fastcgi\_params;  
break;  
}

if ($mygeo = “1”) {  
break;

}

if ($geoip\_country\_code = “DE”) {  
rewrite ^(.\*)$ [http://de.domain.com](http://de.domain.com) permanent;

```
                                           }

```

Still someone with ip from germany cant access the admin section, what  
i`m doing wrong here ?

Thanks.

Posted at Nginx Forum:

> **[Re: Geoip redirect but exclude a specific directory](https://forum.nginx.org/read.php?2,152469,152716#msg-152716)**
>
> Hello, I\`m using the method described in here to redirect people with specific ips to locations \[code\] http { keepalive\_timeout 65; geoip\_country /usr/local/etc/geo/GeoIP.dat ; # these are the GeoIP exceptions for your site geo

---

<div class="post-metadata">

**Author:** ![st1905](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/35a633/32.png) [@st1905](https://www.ruby-forum.com/u/st1905)\
**Post date:** [November 23, 2010, 7:50pm UTC](https://www.ruby-forum.com/t/geoip-redirect-but-exclude-a-specific-directory/199497/4 "2010-11-23T19:50:57Z")

</div>

## Rainer D. Wrote:

> being from Germany?  
> [mailman.nginx.org archive](http://nginx.org/mailman/listinfo/nginx)  
> Thank you for the reply, the ip is dynamic and yes its marked as coming  
> from germany.

Posted at Nginx Forum:  
[http://forum.nginx.org/read.php?2,152469,152735#msg-152735](http://forum.nginx.org/read.php?2,152469,152735#msg-152735)

---

<div class="post-metadata">

**Author:** ![st1905](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/35a633/32.png) [@st1905](https://www.ruby-forum.com/u/st1905)\
**Post date:** [November 23, 2010, 7:28pm UTC](https://www.ruby-forum.com/t/geoip-redirect-but-exclude-a-specific-directory/199497/5 "2010-11-23T19:28:21Z")

</div>

Am 23.11.2010 um 18:27 schrieb st1905:

> Still someone with ip from germany cant access the admin section, what  
> i`m doing wrong here ?

And the IP is in the GeoIP-datablock marked as being from Germany?

Download the csv-version of the database and look it up yourself.

Rainer

---

<div class="post-metadata">

**Author:** ![st1905](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/35a633/32.png) [@st1905](https://www.ruby-forum.com/u/st1905)\
**Post date:** [November 24, 2010, 1:50pm UTC](https://www.ruby-forum.com/t/geoip-redirect-but-exclude-a-specific-directory/199497/6 "2010-11-24T13:50:14Z")

</div>

I solved this by adding following rewrite rule just above the if  
statements for the geoip redirection

if ($request\_uri ~\* ^/admin)  
{  
break;  
}

That solved the issue, now when someone from germany goes to site root  
they will still be redirected but if they go to admin section of the  
site, it wont redirect, thanks a lot Rainer and Maxim.

Posted at Nginx Forum:

> **[Re: Geoip redirect but exclude a specific directory](https://forum.nginx.org/read.php?2,152469,152931#msg-152931)**
>
> Hello, I\`m using the method described in here to redirect people with specific ips to locations \[code\] http { keepalive\_timeout 65; geoip\_country /usr/local/etc/geo/GeoIP.dat ; # these are the GeoIP exceptions for your site geo
