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
To find a location match for an URI, NGINX first scans the locations that is defined using the prefix strings (without regular expression). Thereafter, the location with regular expressions are checked in order of their declaration in the configuration file.
Support for regular expressions is one of the powerful features of NGINX, but regexes can be complex and difficult to get right, especially if you don't work with them regularly. NGINX allows regexes in multiple parts of a configuration, for example locations, maps, rewrites, and server names.
Old thread but I had the same problem...
I think the error isn't related to the PCRE version installed
NGINX doesn't parse your regex if your location tag doesn't start with ~ You need to use something like this
location ~ ^/a/b/(?<myvar>[a-zA-Z]+) { # use variable $myvar here if ($myvar = "sth") { ... } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With