0

Having an nginx location prefix section where the rewrite rule regex starts with the location itself, is there a difference between having the regex with or without the caret (^)? Doesn't seem so, since it will return the first match, but maybe a counterexample exists?

For example, location with ^:

location /smth/ {
    rewrite ^/smth(.*)$ /somewhere/else$1 break;
}

And without:

location /smth/ {
    rewrite /smth(.*)$ /somewhere/else$1 break;
}

Seem functionally equivalent.

renbou
  • 51
  • 1
  • 5
  • Since you are using a [greedy](https://stackoverflow.com/questions/5319840/greedy-vs-reluctant-vs-possessive-qualifiers) `*` quantifier, with the PCRE (or PCRE2) library nginx uses internally there will be no difference (moreover, you can omit the end-of-string anchor `$` too). However used inside the `location /smth/ { ... }` your `$1` variable will always start with the `/` which will give you `/something/else//suffix` for any `/smth/suffix` URI which is probably not that you want, so replace `/somewhere/else/$1` with the `/somewhere/else$1`. – Ivan Shatsky Jan 28 '22 at 16:31
  • I feel like the greedy quantifier doesn't have anything to do with the beginning of the match, rather the end (and you are write about saying that $ is useless in this case. As for the // - it was just an accidental error, I'll fix it in the question but it also does not affect the match itself :) – renbou Jan 28 '22 at 18:39

0 Answers0