# Deny by ip and redirect

**URL:** <https://www.ruby-forum.com/t/deny-by-ip-and-redirect/197625>\
**Category:** NGINX\
**Created:** [October 17, 2010, 4:37pm UTC](https://www.ruby-forum.com/t/deny-by-ip-and-redirect/197625 "2010-10-17T16:37:35Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![okkezSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/85f322/32.png) [@okkezSS](https://www.ruby-forum.com/u/okkezSS)\
**Post date:** [October 17, 2010, 4:37pm UTC](https://www.ruby-forum.com/t/deny-by-ip-and-redirect/197625/1 "2010-10-17T16:37:35Z")

</div>

hi all,

I’d like to only allow a certain set of ip addresses to access one of  
our  
test sites, and redirect anyone with a different ip address to our  
primary  
domain.

location / {  
allow 1.1.1.1;  
deny all;  
}

The above code works as expected, but I would like to do something like  
this:

location / {  
if not 1.1.1.1 or 1.1.1.2 or 1.1.1.3 do a 301 redirect to  
[www.domain.com](http://www.domain.com)(with request uri)  
}

is this possible?

---

<div class="post-metadata">

**Author:** ![iberci](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/a3d4f5/32.png) [@iberci](https://www.ruby-forum.com/u/iberci)\
**Post date:** [October 17, 2010, 5:25pm UTC](https://www.ruby-forum.com/t/deny-by-ip-and-redirect/197625/2 "2010-10-17T17:25:25Z")

</div>

Hi , This is a simple setting .Maybe it can solve your problem .

location / {  
allow 1.1.1.1;  
allow 1.1.1.2;  
allow 1.1.1.3;  
deny all;  
}

error\_page 403 /403.html;  
location=/403.html{  
proxy\_pass [http://www.domain.com](http://www.domain.com);  
}

---

<div class="post-metadata">

**Author:** ![iberci](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/a3d4f5/32.png) [@iberci](https://www.ruby-forum.com/u/iberci)\
**Post date:** [October 17, 2010, 5:29pm UTC](https://www.ruby-forum.com/t/deny-by-ip-and-redirect/197625/3 "2010-10-17T17:29:11Z")

</div>

On Sun, 17 Oct 2010 10:36:42 -0400, Ilan B. wrote:

[…]

> The above code works as expected, but I would like to do something  
> like  
> this:
> 
> location / {  
> if not 1.1.1.1 or 1.1.1.2 or 1.1.1.3 do a 301 redirect to  
> [www.domain.com](http://www.domain.com)(with request uri)  
> }
> 
> is this possible?

Use the nginx geo module to set up a boolean-style variable - say,  
$internal\_ip. Then put something like this in the server block for your  
test site:

if ($internal\_ip != 1) {  
rewrite ^ [http://your.public.site/](http://your.public.site/) permanent;  
}

helen

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

---

<div class="post-metadata">

**Author:** ![iberci](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/a3d4f5/32.png) [@iberci](https://www.ruby-forum.com/u/iberci)\
**Post date:** [October 17, 2010, 6:01pm UTC](https://www.ruby-forum.com/t/deny-by-ip-and-redirect/197625/4 "2010-10-17T18:01:27Z")

</div>

Thank you, I actually resolved it by:

location / {  
allow 1.1.1.1;  
deny all;  
error\_page 403 = @redirect\_towww;  
}

location @redirect\_towww  
{  
rewrite ^(.\*) [http://www.primarydomain.com$1](http://www.primarydomain.com$1) permanent;  
}

---

<div class="post-metadata">

**Author:** ![iberci](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/a3d4f5/32.png) [@iberci](https://www.ruby-forum.com/u/iberci)\
**Post date:** [October 17, 2010, 6:12pm UTC](https://www.ruby-forum.com/t/deny-by-ip-and-redirect/197625/5 "2010-10-17T18:12:46Z")

</div>

On Sun, Oct 17, 2010 at 12:00:09PM -0400, Ilan B. wrote:

> rewrite ^(.\*) [http://www.primarydomain.com$1](http://www.primarydomain.com$1) permanent;  
> }

error\_page 403 http://www.primarydomain.com$request\_uri;

However, you can not set 301 code.

> > > if not 1.1.1.1 or 1.1.1.2 or 1.1.1.3 do a 301 redirect to  
> > > rewrite ^ [http://your.public.site/](http://your.public.site/) permanent;  
> > > [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> > > [mailman.nginx.org archive](http://nginx.org/mailman/listinfo/nginx)

> * * *
> 
> nginx mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [mailman.nginx.org archive](http://nginx.org/mailman/listinfo/nginx)

–  
Igor S.

> **[Igor Sysoev](http://sysoev.ru/en/)**

---

<div class="post-metadata">

**Author:** ![iberci](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/a3d4f5/32.png) [@iberci](https://www.ruby-forum.com/u/iberci)\
**Post date:** [October 17, 2010, 6:19pm UTC](https://www.ruby-forum.com/t/deny-by-ip-and-redirect/197625/6 "2010-10-17T18:19:06Z")

</div>

Is there a way to do it with a 301?

---

<div class="post-metadata">

**Author:** ![iberci](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/a3d4f5/32.png) [@iberci](https://www.ruby-forum.com/u/iberci)\
**Post date:** [October 17, 2010, 6:30pm UTC](https://www.ruby-forum.com/t/deny-by-ip-and-redirect/197625/7 "2010-10-17T18:30:30Z")

</div>

Thanks Igor, appreciate the information, is this something you expect to  
be  
done in coming versions?

For my purposes for now, this is acceptable.

Thanks

---

<div class="post-metadata">

**Author:** ![iberci](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/a3d4f5/32.png) [@iberci](https://www.ruby-forum.com/u/iberci)\
**Post date:** [October 17, 2010, 6:26pm UTC](https://www.ruby-forum.com/t/deny-by-ip-and-redirect/197625/8 "2010-10-17T18:26:42Z")

</div>

On Sun, Oct 17, 2010 at 12:17:55PM -0400, Ilan B. wrote:

> Is there a way to do it with a 301?

It should be implemented as  
error\_page 403 =301 http://www.primarydomain.com$request\_uri;  
but currently this is not implemented.

> > > }  
> > > On Sun, Oct 17, 2010 at 11:28 AM, helen [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> > > 
> > > > > [www.domain.com](http://www.domain.com)(with request uri)  
> > > > > rewrite ^ [http://your.public.site/](http://your.public.site/) permanent;  
> > > > > [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> > > > > Igor S.  
> > > > > [Igor Sysoev](http://sysoev.ru/en/)
> > 
> > * * *
> > 
> > nginx mailing list  
> > [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> > [mailman.nginx.org archive](http://nginx.org/mailman/listinfo/nginx)

> * * *
> 
> nginx mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [mailman.nginx.org archive](http://nginx.org/mailman/listinfo/nginx)

–  
Igor S.

> **[Igor Sysoev](http://sysoev.ru/en/)**

---

<div class="post-metadata">

**Author:** ![iberci](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/a3d4f5/32.png) [@iberci](https://www.ruby-forum.com/u/iberci)\
**Post date:** [October 17, 2010, 6:34pm UTC](https://www.ruby-forum.com/t/deny-by-ip-and-redirect/197625/9 "2010-10-17T18:34:55Z")

</div>

On Sun, Oct 17, 2010 at 12:29:14PM -0400, Ilan B. wrote:

> Thanks Igor, appreciate the information, is this something you expect to be  
> done in coming versions?

The attached patch allows this.  
It will be included in the next 0.8.53.

---

<div class="post-metadata">

**Author:** ![iberci](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/a3d4f5/32.png) [@iberci](https://www.ruby-forum.com/u/iberci)\
**Post date:** [October 17, 2010, 6:43pm UTC](https://www.ruby-forum.com/t/deny-by-ip-and-redirect/197625/10 "2010-10-17T18:43:43Z")

</div>

On Sun, Oct 17, 2010 at 08:33:36PM +0400, Igor S. wrote:

> On Sun, Oct 17, 2010 at 12:29:14PM -0400, Ilan B. wrote:
> 
> > Thanks Igor, appreciate the information, is this something you expect to be  
> > done in coming versions?
> 
> The attached patch allows this.  
> It will be included in the next 0.8.53.

Sorry, this is wrong patch.

–  
Igor S.

> **[Igor Sysoev](http://sysoev.ru/en/)**

---

<div class="post-metadata">

**Author:** ![iberci](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/a3d4f5/32.png) [@iberci](https://www.ruby-forum.com/u/iberci)\
**Post date:** [October 17, 2010, 9:06pm UTC](https://www.ruby-forum.com/t/deny-by-ip-and-redirect/197625/11 "2010-10-17T21:06:41Z")

</div>

Thanks, this would be the first time I’m applying a patch. What’s the  
correct process?

2010/10/17 Igor S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![iberci](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/a3d4f5/32.png) [@iberci](https://www.ruby-forum.com/u/iberci)\
**Post date:** [October 17, 2010, 7:41pm UTC](https://www.ruby-forum.com/t/deny-by-ip-and-redirect/197625/12 "2010-10-17T19:41:02Z")

</div>

On Sun, Oct 17, 2010 at 08:42:56PM +0400, Igor S. wrote:

> Sorry, this is wrong patch.  
> The new patch.

---

<div class="post-metadata">

**Author:** ![iberci](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/a3d4f5/32.png) [@iberci](https://www.ruby-forum.com/u/iberci)\
**Post date:** [October 17, 2010, 9:38pm UTC](https://www.ruby-forum.com/t/deny-by-ip-and-redirect/197625/13 "2010-10-17T21:38:52Z")

</div>

On Sun, Oct 17, 2010 at 03:05:44PM -0400, Ilan B. wrote:

> Thanks, this would be the first time I’m applying a patch. What’s the  
> correct process?

tar zxf nginx-0.8.52.tar.gz  
patch -d nginx-0.8.52 \< patch.error\_page.30x.1  
cd nginx-0.8.52  
./configure …

> > > > > done in coming versions?  
> > > > > Igor S.  
> > > > > [Igor Sysoev](http://sysoev.ru/en/)
> > 
> > * * *
> > 
> > nginx mailing list  
> > [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> > [mailman.nginx.org archive](http://nginx.org/mailman/listinfo/nginx)

> * * *
> 
> nginx mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [mailman.nginx.org archive](http://nginx.org/mailman/listinfo/nginx)

–  
Igor S.

> **[Igor Sysoev](http://sysoev.ru/en/)**

---

<div class="post-metadata">

**Author:** ![iberci](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/a3d4f5/32.png) [@iberci](https://www.ruby-forum.com/u/iberci)\
**Post date:** [October 18, 2010, 6:00am UTC](https://www.ruby-forum.com/t/deny-by-ip-and-redirect/197625/14 "2010-10-18T06:00:59Z")

</div>

I think I applied the patch correctly, ran patch \< patchfilename inside  
the  
src/http directory and looks like the files were patched. I then  
modified  
the config file to have:

error\_page = 301 http://www.myprimarydomain.com$request\_uri;

When starting nginx, I now get this error:

Starting nginx: [emerg]: invalid value “=” in /etc/nginx/nginx.conf:42

2010/10/17 Igor S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![iberci](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/a3d4f5/32.png) [@iberci](https://www.ruby-forum.com/u/iberci)\
**Post date:** [October 18, 2010, 6:55am UTC](https://www.ruby-forum.com/t/deny-by-ip-and-redirect/197625/15 "2010-10-18T06:55:29Z")

</div>

On Sun, Oct 17, 2010 at 11:59:54PM -0400, Ilan B. wrote:

> I think I applied the patch correctly, ran patch \< patchfilename inside the  
> src/http directory and looks like the files were patched. I then modified  
> the config file to have:
> 
> error\_page = 301 http://www.myprimarydomain.com$request\_uri;

error\_page 403 =301 http://www.myprimarydomain.com$request\_uri;

> > > 
> > 
> > nginx mailing list  
> > [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> > [mailman.nginx.org archive](http://nginx.org/mailman/listinfo/nginx)

> * * *
> 
> nginx mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [mailman.nginx.org archive](http://nginx.org/mailman/listinfo/nginx)

–  
Igor S.

> **[Igor Sysoev](http://sysoev.ru/en/)**

---

<div class="post-metadata">

**Author:** ![iberci](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/a3d4f5/32.png) [@iberci](https://www.ruby-forum.com/u/iberci)\
**Post date:** [October 17, 2010, 10:54pm UTC](https://www.ruby-forum.com/t/deny-by-ip-and-redirect/197625/16 "2010-10-17T22:54:23Z")

</div>

I get this error:

## can’t find file to patch at input line 5 Perhaps you should have used the -p or --strip option? The text leading up to this was:

|Index: src/http/ngx\_http\_core\_module.c  
|===================================================================  
|— src/http/ngx\_http\_core\_module.c (revision 3099)

| +++ src/http/ngx\_http\_core\_module.c (working copy) |
| --- |

File to patch:
