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

  deny all;

if ($example2){
  allow all;

1 Answer 1


You can do this by using the geo module. This will only allow ip and 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;

    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.