Kees Schepers
Kees Schepers

Reputation: 2248

Regex in nginx location match extension without "resolve"

I am not a god in Regular Expressions and stuck with a complex location regex.

I have a location block in my nginx config to make sure images and other media are cached. But if "resolve" is in the URL it should not match and skip the block. I can't get it to work. I have looked at a negative lookbegind but I think I do not completely understand how it works.

This is my block:

location ~* \.(?:css|js|gif|jpeg|jpg|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc)$ { //caching stuff }

It should match the following URL:

/media/cache/ps_product_admin_thumb/productimages/image.jpg

But not the following:

/media/cache/resolve/ps_product_admin_thumb/productimages/image.jpeg

Anyone who can send me in the right direction?

Upvotes: 2

Views: 248

Answers (1)

Ryszard Czech
Ryszard Czech

Reputation: 18611

A lookahead may help:

^(?!.*resolve).*\.(?:css|js|gif|jpeg|jpg|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc)$

(?!.*resolve) makes sure no resolve exists in the string.

See proof & explanation

Upvotes: 2

Related Questions