I've spent some time trying to find the answer before writing this question:

I have several location blocks in my nginx config file that look like this:

location ~ ^/(doc|img|bla|foo)/metadata/$ { ... }

location ~ ^/(doc|img|bla|foo)/deleted/$ { ... }

location ~ ^/(doc|img|bla|foo)/something/$ { ... }

So I thought it would be a good idea to refactor the repeated regular expression into a variable that I set in the server section (also because I know I'll have to add to it in the future), like so:

server {
     set $my_regex doc|img|blah|foo;

     # I also tried this one:
     set $my_regex "doc|img|blah|foo";

Which I would then reuse inside the location blocks:

# this doesn't have any effect
location ~ ^/($my_regex)/metadata/$ { ... }

# this one neither
location ~ ^/(\$my_regex)/deleted/$ { ... }

# and this one neither
location ~ ^/(${my_regex})/something/$ { ... }

Any idea? Thanks!

  • Greetings! Just interesting: did You try to combine two expressions in one? location ~ ^/(doc|img|bla|foo)/(metadata|deleted|something)/$ { ... } Feb 20, 2018 at 17:39
  • @SergeySerov No, I'm not going that far. I'm just trying to refactor the regular expression that has been used three times by assigning it to a variable that I then would like to use in the location directive.
    – jfix
    Feb 20, 2018 at 17:59
  • @SergeySerov Actually I now have combined them into one, and use a rewrite. But that doesn't solve the initial problem. :-)
    – jfix
    Feb 20, 2018 at 22:15
  • Now You have one line instead three, this is good already. Yes, the task with string variable not resolved yet. It is interesting task for me too ;) Feb 20, 2018 at 22:29

2 Answers 2


There is no way to use variables in location matching.

If you want it, you probably doing something wrong. In this case you could try nesting locations.

  • @AlexeyTan I've reduced the use of the regex to only two occurrences which will make the pain of maintaining somewhat more bearable. But I guess I have to find another solution, maybe scripting?
    – jfix
    Feb 20, 2018 at 22:16

Please check at this link as they said in NGINX documentation

The PCRE library supports named captures using the following syntax:

?<name> Perl 5.10 compatible syntax, supported since PCRE-7.0
?'name' Perl 5.10 compatible syntax, supported since PCRE-7.0
?P<name>    Python compatible syntax, supported since PCRE-4.0

Named captures are a feature of PCRE and they have different syntax available in different versions. For the syntax you use ? you must have PCRE 7.0 at least.

  • I don't think I'm looking for named captures. From what I understand, named captures allow to reference a matched part of a regex by name rather than by a numeric index in later processing. Here however I'm looking for the possibility to reuse an existing variable inside a regex. Maybe that's just not possible?
    – jfix
    Feb 20, 2018 at 17:25

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.