2

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.

Felipe Zavan
  • 1,654
  • 1
  • 14
  • 33
  • Duplicate of https://serverfault.com/questions/816998/nginx-rule-match-all-paths-except-one – muradm Dec 17 '18 at 01:48

1 Answers1

2

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

Tim
  • 2,756
  • 1
  • 15
  • 31