Redirecting all requests that aren't from my IP with nginx

http, nginx, redirect

Solution

If this `if` is created inside the `location /` for example, create a separate `location /some-page` this way the `if` won't be executed when the URI is `/some-page`

EDIT: ok let me explain what i understood and you tell me if i'm right or wrong,

- Good IP (yours): serve page as it is

- Bad IP (not yours): redirect to `/some-page`

The problem is, when Bad IP is redirected to `/some-page` it still redirects to `/some-page` again because it's still a Bad IP, so it passes the `if` test

My solution: Remove the `/some-page` location from the `/` block:

location / {
    # bla bla
    if ($remote_addr != 127.0.0.1) {
        rewrite ^ http://www.example.com/some-page;
    }
    # rest of bla bla
}
location /some-page {
    try_files index.html index.php; # or whatever
}

When Bad IP is forwarded to `/some-page` it no longer will execute the `if` condition, so that will end the infinite redirection loop.

Second EDIT: You could set the permissions in nginx it self, let me demonstrate:

location / {
    error_page 403 = @badip
    allow 127.0.0.1;
    deny all;
    #rest of bla bla
}
location @badip {
    return 301 $scheme://example.com/some-page;
}

Problem

I want all nginx requests that aren't made by my IP address to be redirected to `/some-page`. I'm currently using this nginx config: ``` if ($remote_addr != 127.0.0.1) { rewrite ^ http://www.example.com/some-page; } ``` This works for me as I'm not redirected, but anyone else is stuck in a redirect loop since the block doesn't check if the request is for `/some-page`. How can I fix this? I'm not sure how to check the request path.

Original source