Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

In my Nginx config I have

location / {
allow 198.168.0.1;
deny all;
}

Now, I want to redirect all other IPs except the allowed one to google.com, how do I do that ?

I have tried something like

location / {
allow 198.168.0.1;
deny all;
rewrite ^ http://google.com/;
}

but this redirects directly to google.com for all IP. I want only 198.168.0.1 to access the page, others would be redirected.

share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

Use the error_page directive for this.

location / {
    allow 198.168.0.1;
    deny all;
    error_page 403 http://www.google.com/;
}

Since the deny all will serve a 403 error, we override the 403 handling by sending a redirect to http://www.google.com/.

share|improve this answer
    
thank you... great explanation too !!! –  Jhilke Dai Mar 25 '13 at 14:24
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.