nginx缓存检查 - 采用try_files

我有一个图像服务器,可以写入缓存目录(它只是在当前请求的 URL路径中使用了相同的路径)。我已经确认在初始请求(即缓存中没有任何文件时)会写入新文件,但在随后的请求中,'try_files' 无法成功找到该文件。

我的 'nginx.conf' 如下:

http {
    server {
        listen 8080;
        # 如果不在缓存中,则提供tiles
        location @image_server {
          content_by_lua_file "/ Users/tim/work/chickpea/serve_image.lua";
        }

        # 捕获tile请求,例如/tile/l8/091080/rgb/20160924/10/938/597.jpg
        # 每个参数都有命名捕获组的正则表达式
        location ~ ^/tile/(?P<layer>[^/]+)/(?P<pathrow>[^/]+)/(?P<type>[^/]+)/(?P<date>[^/]+)/(?P<z>[^/]+)/(?P<x>[^/]+)/(?P<y>[^.]+) {
          root cache;
          set $cachepath "cache/$layer/$pathrow/$type/$date/$z/$x/$y.jpg";
          try_files $cachepath @image_server;
          add_header X-Static hit;
        }

        ..

我已经确认对 /tile/l8/091080/rgb/20160924/10/938/597.jpg 的请求,$cachepathcache / l8 / 091080 / rgb / 20160924 / 10 / 938 / 597.jpg,并且 597.jpg 在正确的路径中。我尝试过 $cachepath 的语法,包括和不包括开头的 cache /,但无法解决问题。

点赞
用户127660
用户127660

有两个更改需要使其正常工作:

  1. root 指令变为绝对路径,否则它会变成 /usr/local/nginx/cache

  2. $cachepath 变量前添加一个前导斜杠。因为 root 指令的值和 $cachepath 会被连接在一起以确定完整的文件路径。

2016-10-02 03:09:15