0

So I have a regex-based location rule, something like this:

location ~ /(abc.*xyz|def.*zxy|ghk.*qwe)/

Can I check which part of regex yielded the match? Meaning that if the url was like

/12abc34/

Than I'd like to know that it was matched by

.*abc.*

Is there a way?

I'm running latest nginx compiled with lua-module on a Debian VM on AWS.

------------Update-------------

I'm thinking to use inner location, but it will be ugly:

 location ~ /(abc.*xyz|def.*zxy|ghk.*qwe)/
{
    location ~ /(abc.*xyz){...}
    location ~ /(def.*zxy){...}
...
}

Right now I have like 60 regex patterns. Is such approach ok?

1
  • Have you considered using map with $request_uri? See this document for details. Commented Nov 27, 2016 at 15:23

1 Answer 1

1

Your regular expression expression: /(.*abc.*|.*def.*|.*ghk.*)/ can be reduced to:

location ~ (abc|def|ghk).*/ { ... }

As any nginx URI begins with a /, you do not need to test for /.*.

The part of the regular expression within parentheses is captured as $1 within the location block.

So in the case of /12abc34/, the variable $1 would be set to abc.

Sign up to request clarification or add additional context in comments.

1 Comment

Ok, but I also have rules like (xyz.*abc). The solution you offered unfortunatelly will not work for this :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.