Answer a question

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?

Answers

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

Logo

开发云社区提供前沿行业资讯和优质的学习知识,同时提供优质稳定、价格优惠的云主机、数据库、网络、云储存等云服务产品

更多推荐