# Limit to IP, redirect all others to alternate site?

**URL:** <https://www.ruby-forum.com/t/limit-to-ip-redirect-all-others-to-alternate-site/186007>\
**Category:** NGINX\
**Created:** [March 30, 2010, 11:37am UTC](https://www.ruby-forum.com/t/limit-to-ip-redirect-all-others-to-alternate-site/186007 "2010-03-30T11:37:10Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![phillipoertel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/4af34b/32.png) [@phillipoertel](https://www.ruby-forum.com/u/phillipoertel)\
**Post date:** [March 30, 2010, 11:37am UTC](https://www.ruby-forum.com/t/limit-to-ip-redirect-all-others-to-alternate-site/186007/1 "2010-03-30T11:37:10Z")

</div>

How would I limit a virtual host to a single IP and redirect all other  
IPs to an alternate domain/website? I’m using the http\_access module to  
limit IPs which returns a 403, but I’d prefer that nginx return a  
“location” header to bounce visitors to [http://foo.com](http://foo.com).

At the moment I’ve got this:

server {  
if ( $remote\_addr != 1.1.1.1 ) {  
rewrite ^ [http://foo.com](http://foo.com) last;  
}

```
 location / {
     allow 1.1.1.1;
     deny all;
 }

```

}

but that feels quite “hacky”. Is there a better format?

---

<div class="post-metadata">

**Author:** ![phillipoertel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/4af34b/32.png) [@phillipoertel](https://www.ruby-forum.com/u/phillipoertel)\
**Post date:** [March 30, 2010, 11:44am UTC](https://www.ruby-forum.com/t/limit-to-ip-redirect-all-others-to-alternate-site/186007/2 "2010-03-30T11:44:12Z")

</div>

Actually, just saw that error\_page can take a URL as an argument.  
Changed to the following:

server {  
error\_page 403 [http://foo.com](http://foo.com);  
location / {  
allow 1.1.1.1;  
deny all;  
}  
}

Works perfectly.

Thanks!
