Let’s say I have a URL like this: www.example.com/a/b/sth, and I write a location block in Nginx config:
location ^~ /a/b/(?<myvar>[a-zA-Z]+) {
# use variable $myvar here
if ($myvar = "sth") { ... }
}
I hope to be able to use variable $myvar captured from the URL inside the block, however, Nginx keeps telling me this variable is not defined and won’t start:
nginx: [emerg] unknown "myvar" variable
As Stefano Fratini correctly pointed out in his answer, your
locationdeclaration has an error: for regular expressions you should use~alone, not^~.Named captures are a feature of PCRE and they have different syntax available in different versions. For the syntax you use
?<var>you must have PCRE 7.0 at least.Please see the extensive information in official Nginx documentation