Nginx rule – match all paths except one

nginxregex

I am trying to match all paths that begin with /newsletter/ except one (/newsletter/one) with a regex.
What I have so far:

   location ~ ^/newsletter/(.*)$ {
// configuration here
}

This matches all the paths that begin with /newsletter/.

How do I make an exception for the path /newsletter/one ?

Best Answer

I ended up using the following solution:

location ~ ^/newsletter/(.*)$ {
    location ~ /newsletter/one(.*) {
           // logic here
    }
    // logic here
}

This matches all the paths under /newsletter/* and then I match all the paths that begin with /newsletter/one and apply the configuration for the newsletter/one in the inner configuration block while I keep the rest of the configuration in the outer configuration block.