Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nginx location's regex, is escaping the forward slash needed?

Nginx uses the PCRE engine for evaluating regular expressions, the documentation state that / delimiter is not used so we don’t have to escape the forward slash / in an URI as we may do in a standard regex. An example of a valid nginx regex is location ~* /myapp/.+\.php$

BUT the following code is escaping the forward slash

location ~ ^\/(?:index|core\/ajax\/update|ocs\/v2|ocm-provider\/.+)\.php(?:$|\/)

What does \/ exactly mean in that context and why is it needed when the documentation says otherwise?

like image 245
intika Avatar asked Oct 16 '22 07:10

intika


1 Answers

While Nginx does not require escaping the forward slash / it does not either deny escaping it like we could escape any other character.

The first purpose of the regex special character \ is meant to escape the next character and thus nginx is just translating/matching \/ to / like it would translate/match \j to literal j (the example have no purpose never the less \j match literal j).

One purpose of escaping forward slashes in the context of nginx could be for code portability.

Note that \ followed by a character have probably a different meaning than just escaping the followed char, a complete list is available here

Source: @monkeyzeus and @richard-smith comments

like image 151
intika Avatar answered Oct 27 '22 20:10

intika