1

Is it possible to conditionally allow or deny IP in NGINX? This isn't allowed.

if($example){
  deny all;
}

if ($example2){
  allow all;
}

1 Answer 1

5

You can do this by using the geo module. This will only allow ip 1.1.1.1 and 2.2.2.2 to access the endpoint. You could reverse the logic to allow all ips and only deny a specific few.

geo $trusted_user {
    default 0;
    1.1.1.1 1;
    2.2.2.2 1;
}

server
{
    if ( $trusted_user ~ 0 ) { return 444; }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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