Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nginx location match multiple extensions unless path starts with specific word

How can I write a location block that matches any path ending in the following extensions:

jpg|jpeg|gif|css|png|js|ico|json|xml|txt|html

Unless the path starts with "/rails" (eg: /rails/randomstring/image.png)?

I currently have this basic block:

location ~* \.(jpg|jpeg|gif|css|png|js|ico|json|xml|txt|html)$ {
  gzip_static on;
  gzip on;
  expires max;
  add_header Cache-Control public;
}

But this would match "/rails/randomstring/image.png" and I don't want that.

like image 388
Felipe Zavan Avatar asked Nov 24 '25 07:11

Felipe Zavan


1 Answers

You can anchor a negative lookahead to the beginning of the string:

^(?!\/rails).*(?:jpg|jpeg|gif|css|png|js|ico|json|xml|txt|html)

Demo

like image 66
Tim Avatar answered Nov 26 '25 23:11

Tim



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!