Abort request and `return;` based on variable (without using if?)

by aexl   Last Updated October 09, 2019 14:00 PM

What I have:

# nginx.conf
http {
    map "$request_uri" $abort {
        default 0;
        "~*example" 1;
    }
    include sites-enabled/*.conf;
    # etc
}
# sites-enabled/example.com.conf
server {
    listen 80;
    location ~ \.js {
        if ($abort = 1) {
            return 500;
        }
    }
    # etc
}
# sites-enabled/example.net.conf
# sites-enabled/example.org.conf
# etc.

What is the problem:

  • I used map because if is evil. Now, $abort has a flag, knowing whether to proceed or not.
  • I don't know how to utilise that variable other than in an if statement. These comments confirm.
  • An if is allowed only no deeper than the server{} context. I have 12 sites, 3 server{}s each.

What I want:

Evaluate $abort and tell Nginx to return 500 somewhere on the shared http {} level.

Avoid repeating the if for every server {} of every virtual host.

I know I can put the if into e.g. snippets/abort.conf and include it, but that seems like cluttering and waste of resources. I hope there is a more elegant way. Do you know any?

Tags : nginx


Related Questions


Updated September 16, 2019 18:00 PM

Updated August 03, 2019 14:00 PM

Updated March 27, 2015 07:00 AM

Updated March 26, 2015 14:00 PM