logical or in location directive with regex (multiple location using same block)

Roger Fischer roger at netskrt.io
Fri Nov 23 23:38:27 UTC 2018


how do I best handle multiple locations that use the same code block? I do not want to repeat the { … } part. Is there a way to do this with logical or between regular expressions?

The first variation is that sometimes there are query parameters, and sometimes there are not. The second variation is that there are multiple suffixes that should match the same block.

As an example, the following locations all should have the same behaviour:
* /variable-part.gif
* /variable-part.gif?variable-query-parameters
* /variable-part.png
* /variable-part.png?variable-query-parameters

The equivalent regular expressions I am using are:
* ~* \.gif$
* ~* \.gif\?
* ~* \.png$
* ~* \.png\?

I know, I can combine the different types:
* ~* \.(gif|png)$
* ~* \.(gif|png)\?

But I don’t know how to combine the end-of-uri with the followed-by-query-parameters into a single regex.

Lastly, I also have locations with a quite different pattern that has the same code block.

So, what I would like is something like this:

location  (  ~* \.(gif|png)$  |  ~* \.(gif|png)\?  |  = /xyz ) { … }

“|” represents a logical or.

Is there a way to do this?

BTW, should the regular expression be in quotes (single or double)?



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.nginx.org/pipermail/nginx/attachments/20181123/50f9a297/attachment.html>

More information about the nginx mailing list