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?

Alex L.
  • 101
  • 11
  • Have you considered using `map` with `$request_uri`? See [this document](http://nginx.org/en/docs/http/ngx_http_map_module.html#map) for details. – Richard Smith Nov 27 '16 at 15:23

1 Answers1

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.

Richard Smith
  • 45,711
  • 6
  • 82
  • 81
  • Ok, but I also have rules like (xyz.*abc). The solution you offered unfortunatelly will not work for this :( – Alex L. Nov 27 '16 at 15:02