【问题标题】:Configure nginx to statically cache certain URLs配置 nginx 静态缓存某些 URL
【发布时间】:2013-05-22 05:43:19
【问题描述】:

我想通过直接查找静态文件来缓存我的应用程序中的某些 URL。但是,静态缓存的文件可能不存在,所以我只想在文件存在的情况下重定向请求。我想我可以以某种方式使用 try_files,但到目前为止我还没有成功地做到这一点。

这是我尝试过的:

location ^/(.*)/(.*)/other/stuff/(.*)/this-is-static {
  try_files /static-cache/$1/$2/$3/this-is-static @app;
}

static-cache 是在同一文件中配置的内部位置。这似乎没有按预期工作。怎么了?

我得到的最接近的是这个,它会重写每个请求(因此当文件没有被缓存时会失败):

location ^/(.*)/(.*)/other/stuff/(.*)/this-is-static {
  rewrite ^ /static-cache/$1/$2/$3/this-is-static;
}

【问题讨论】:

  • 在执行正则表达式时,您应该使用 ~ location ~ /bla(.*)bla { 开始位置块

标签: file caching nginx rewrite try-catch


【解决方案1】:

你试过~吗?我也用[^/]来避免贪婪的正则表达式

location ~ /([^/]*)/([^/]*)/other/stuff/([^/]*)/this-is-static {
    try_files /static-cache/$1/$2/$3/this-is-static @app;
}
location @app {
    #handle non cached files
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-02
    • 2020-02-08
    • 2020-09-17
    • 2016-06-27
    • 1970-01-01
    • 2022-06-11
    • 2023-01-20
    相关资源
    最近更新 更多