8

So I was configuring nginx today and I hit a weird problem. I was trying to match a location like this:

location ~ ^/([0-9]+)/(.*) {
    # do proxy redirects
}

...for URLs like "http://my.domain.com/0001/index.html".

This rule was never matching, despite the fact that it by all rights should. It took me awhile to figure out, based on this documentation, that some characters in regexes need to be quoted. The problem is, the documentation is for rewrites, and it specifically calls out curly braces, not square brackets. After a fair bit of experimentation that involved a lot of swearing, I discovered that I could fix the problem by quoting the regex like so:

location ~ "^/([0-9]+)/(.*)" {
    # do proxy redirects
}

Is there a list somewhere of characters that nginx requires quoting regexes with? Or could there be something else going on here that I'm totally missing? This is my first nginx configuration job, so it's very possible I've misunderstood something...

1 Answers1

1

I answered this for Apache recently, but it is a lot easier with Nginx because it uses PCRE (Perl Compatible Regular Expressions) for regex matching, however, certain characters and constructs within regex patterns need special handling, especially when they might conflict with Nginx configuration syntax.

Characters and cases where quoting might be necessary in Nginx regex patterns:

 { } [ ] ( ) $ . ^ \ + * | 

Notice: I could have missed some! The link in your questions isn't working, so I could not verify that.

Good luck!

Max Haase
  • 1,123