6

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...

  • 2
    Try with ^/(\d+)/(.*) to confirm whether it's the brackets or not. – Shane Madden Oct 7 '11 at 17:25
  • That would explain lot of confusion :) – tommics Sep 2 '14 at 10:13
  • If anyone comes across this question, here's the possible related question... serverfault.com/a/793557 – Pothi Kalimuthu Feb 7 '17 at 13:58
0

Do you need the number? I'd try a simpler variant first and see if the error is maybe something else.

I'd try if ^/[0-9]+/, or do you need the captured elements?

| improve this answer | |

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.