Welcome! Log In Create A New Profile

Advanced

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

Roger Fischer
November 23, 2018 06:40PM
Hello,

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)?

Thanks…

Roger


_______________________________________________
nginx mailing list
nginx@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx
Subject Author Posted

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

Roger Fischer November 23, 2018 06:40PM

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

Francis Daly November 24, 2018 04:22AM



Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 200
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready