# How can I redirect some IP addresses?

**URL:** <https://www.ruby-forum.com/t/how-can-i-redirect-some-ip-addresses/221116>\
**Category:** NGINX\
**Created:** [July 1, 2012, 6:36am UTC](https://www.ruby-forum.com/t/how-can-i-redirect-some-ip-addresses/221116 "2012-07-01T06:36:26Z")\
**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:** [July 1, 2012, 6:36am UTC](https://www.ruby-forum.com/t/how-can-i-redirect-some-ip-addresses/221116/1 "2012-07-01T06:36:26Z")

</div>

Hi forulisters,

I’ve got a bunch of IP addresses and IP ranges I’d like  
to redirect to, let’s say the amnesty international site  
(they’re mainly from gov).

I read [http://wiki.nginx.org/HttpAccessModule](http://wiki.nginx.org/HttpAccessModule) but as I’ve  
got many of them I’m wondering if nginx can read a text  
file (or something else) instead of put all these addresses  
in a block?

I’d also like if I can have a round-robin of redirections  
(once for amnesty, next to copwatch, etc)?

Jiff

Posted at Nginx Forum:

> **[How can I redirect some IP addresses?](https://forum.nginx.org/read.php?2,228151,228151#msg-228151)**
>
> Hi forulisters, I've got a bunch of IP addresses and IP ranges I'd like to redirect to, let's say the amnesty international site (they're mainly from gov). I read http://wiki.nginx.org/HttpAccessModule but as I've got many of them I'm wondering if

---

<div class="post-metadata">

**Author:** ![Jiff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dc4da7/32.png) [@Jiff](https://www.ruby-forum.com/u/Jiff)\
**Post date:** [July 1, 2012, 11:56am UTC](https://www.ruby-forum.com/t/how-can-i-redirect-some-ip-addresses/221116/2 "2012-07-01T11:56:19Z")

</div>

On Sun, Jul 01, 2012 at 12:16:31AM -0400, Jiff wrote:

Hi there,

> I read [GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com · GitHub](http://wiki.nginx.org/HttpAccessModule) but as I’ve  
> got many of them I’m wondering if nginx can read a text  
> file (or something else) instead of put all these addresses  
> in a block?

> **[Core functionality](http://nginx.org/en/docs/ngx_core_module.html#include)**
>
> Includes another file, or files matching the specified mask, into configuration. Included files should consist of syntactically correct directives and blocks.

> I’d also like if I can have a round-robin of redirections  
> (once for amnesty, next to copwatch, etc)?

I’m not aware of a pure nginx.conf way of doing that.

But it shouldn’t be too tough to use some scripting – either error\_page  
to a location which does something\_pass to a separate server which  
issues  
the http redirect; or use one of the embedded scripting languages.

Good luck with it,

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Jiff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dc4da7/32.png) [@Jiff](https://www.ruby-forum.com/u/Jiff)\
**Post date:** [July 3, 2012, 9:13pm UTC](https://www.ruby-forum.com/t/how-can-i-redirect-some-ip-addresses/221116/3 "2012-07-03T21:13:13Z")

</div>

Thanks to the both of you, I’ll stick to include + a cron script to  
exchange include files.  
Regards.  
Jiff

Posted at Nginx Forum:

> **[Re: How can I redirect some IP addresses?](https://forum.nginx.org/read.php?2,228151,228259#msg-228259)**
>
> Hi forulisters, I've got a bunch of IP addresses and IP ranges I'd like to redirect to, let's say the amnesty international site (they're mainly from gov). I read http://wiki.nginx.org/HttpAccessModule but as I've got many of them I'm wondering if

---

<div class="post-metadata">

**Author:** ![Jiff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dc4da7/32.png) [@Jiff](https://www.ruby-forum.com/u/Jiff)\
**Post date:** [July 2, 2012, 12:17pm UTC](https://www.ruby-forum.com/t/how-can-i-redirect-some-ip-addresses/221116/4 "2012-07-02T12:17:43Z")

</div>

On Sun, Jul 01, 2012 at 12:16:31AM -0400, Jiff wrote:

> Hi forulisters,
> 
> I’ve got a bunch of IP addresses and IP ranges I’d like  
> to redirect to, let’s say the amnesty international site  
> (they’re mainly from gov).
> 
> I read [GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com · GitHub](http://wiki.nginx.org/HttpAccessModule) but as I’ve  
> got many of them I’m wondering if nginx can read a text  
> file (or something else) instead of put all these addresses  
> in a block?

I think you want the geo module instead:

> **[Module ngx\_http\_geo\_module](http://nginx.org/en/docs/http/ngx_http_geo_module.html)**

It supports the “include” directive inside the “geo” block.  
(Currently it lacks the IPv6 support though.)

> I’d also like if I can have a round-robin of redirections  
> (once for amnesty, next to copwatch, etc)?

While not exactly what you want, but you can use the “split  
clients” module to match $remote\_port to a particular URL:

> **[Module ngx\_http\_split\_clients\_module](http://nginx.org/en/docs/http/ngx_http_split_clients_module.html)**

(This has a limitation: it will always give you the same  
redirection URL over one persistent connection.)

http {  
geo $wrapped {  
127.0.0.1 1;

# include conf/wrapped.conf;

```
}

split_clients "${remote_port}AAA" $redirect {

```

33% [http://url1](http://url1);  
33% [http://url2](http://url2);

- [http://url3](http://url3);  
}

```
server {

```

if ($wrapped) {  
return 301 $redirect;  
}

location / {  
return 200 “normal content”;  
}  
}  
}
