【问题标题】:Nginx location match multiple extensions unless path starts with specific wordNginx 位置匹配多个扩展名,除非路径以特定单词开头
【发布时间】:2018-12-17 12:39:55
【问题描述】:

我如何编写一个与以以下扩展名结尾的任何路径匹配的位置块:

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

除非路径以“/rails”开头(例如:/rails/randomstring/image.png)?

我目前有这个基本块:

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

但这会匹配“/rails/randomstring/image.png”,我不希望这样。

【问题讨论】:

标签: regex nginx regex-negation nginx-location nginx-config


【解决方案1】:

您可以将否定的前瞻锚定到字符串的开头:

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

Demo

【讨论】:

  • 我之前尝试过否定的前瞻,但我一定做错了什么,这个完美。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-02-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-14
  • 2017-04-25
相关资源
最近更新 更多