How to use Nginx Regexp in the location How to use Nginx Regexp in the location nginx nginx

How to use Nginx Regexp in the location


Escaping the braces in the limiting quantifiers is necessary in POSIX BRE patterns, and NGINX does not use that regex flavor. Here, you should not escape the limiting quantifier braces, but you need to tell NGINX that you pass the braces as a part of the regex pattern string.

Thus, you need to enclose the whole pattern with double quotes:

Use

location ~ "/img/([0-9a-fA-F]{2})([0-9a-fA-F]+)$"

Here is a regex demo.

Note that in the current scenario, you can just repeat the subpattern:

 /img/([0-9a-fA-F][0-9a-fA-F])([0-9a-fA-F]+)$      ^^^^^^^^^^^^^^^^^^^^^^^^


I got it working putting double-quotes around the location part like this:

location ~ "/img/([0-9a-f]{2})([0-9a-f]+)$"

the reason is that Nginx uses curly brackets to define config blocks so it thinks is opening a location block.

Source