Limit to IP, redirect all others to alternate site?

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.

At the moment I’ve got this:

server {
if ( $remote_addr != 1.1.1.1 ) {
rewrite ^ http://foo.com last;
}

 location / {
     allow 1.1.1.1;
     deny all;
 }

}

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

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

server {
error_page 403 http://foo.com;
location / {
allow 1.1.1.1;
deny all;
}
}

Works perfectly.

Thanks!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs