The HttpMapModule documentation states that:

Since 1.0.4, case insensitive regular expressions can be used by prefixing the pattern with ~*.

And gives the example:

map $uri $myvalue {
    /aa                   /mapped_aa;
    ~^/aa/(?<suffix>.*)$  $suffix;

I updated to nginx/1.2.7 to make use of this, and naive regexes work okay, but variables don't. When I try the above syntax I get the error:

nginx: [emerg] pcre_compile() failed: unrecognized character after (?< in "^/aa/(?<suffix>.*)$" at "suffix>.*)$"

I also tried the following syntaxes, but none of them worked:

~^(.*)$  $1;
~^(.*)$  \1;

but neither of them worked either.

Does anyone know how to get variables to work in regular expressions in HttpMapModule?

  • Your example works for me with nginx 1.1.19 on Ubuntu 12.04. – mgorven Feb 26 '13 at 4:48
  • @mgorven that's odd. Could you confirm this is definitely true? Maybe it changed between 1.1.19 and 1.2.7. In which case I was wrong to update the documentation: wiki.nginx.org/HttpMapModule#map – Robin Winslow Feb 26 '13 at 16:07
  • Yup, I copy/pasted your example into my config. – mgorven Feb 26 '13 at 16:43

(?P<name>pattern) is the standard PCRE syntax for named capture-groups - the documentation is missing a P.

The "Named Subpatterns" section on Wikipedia states that (?<name>...) and (?'name'...) are valid for PCRE 7.0 onwards; presumably your version of nginx is linked against an earlier version of PCRE.

  • Wonderful. That is the answer. How did you know that? Where would I find PCRE documentation that explains P variables? (I did search for it) – Robin Winslow Feb 26 '13 at 14:19
  • I've updated the documentation: wiki.nginx.org/HttpMapModule – Robin Winslow Feb 26 '13 at 16:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.