Grab value from nginx location path

I find your original question and your comment to the answer a bit contradictory. But to answer your requirement from the comment, you can use a location block like this:

location ~ ^/(users)/v2/(.+)$ {
    proxy_pass http://app.domain.com/api/$1/v2/$2;
}

I assume that the paths also contain other things after the /v2/ here. I capture the other stuff into $2 to use in the proxy_pass.

If you want to allow only specific strings in addition of users, you can use the | rule for OR match:

location ~ ^/(users|admins)/v2/(.+)$

A generic rule for regex captures in nginx is that any regular expression inside parentheses is captured in a variable. First parentheses content is captured into $1 and so on.

Of course there are exceptions to the rule. (?:users) makes nginx not capture the value. (?<variable>users) captures the value into variable called $variable.

Tags:

Nginx