首页 > 关于 `nginx` 的 `cache` 的问题

关于 `nginx` 的 `cache` 的问题

我有一个请求: http://www.example.com/app?test=1 . 希望当有 test=1 这个请求参数的时候缓存这这个url. 请问 nginx 能做到么?。

我在配置文件中写

service {

    location /app {
      if ( $arg_test ) {
        //nginx 的 cache 的配置
      }
    }
}

但是 启动的时候报错。告诉我 cache 不能配置在 if 块中。。。


首先,你当然不能将全部的配置放在if中

不过你可以这样

location /app {
    proxy_set_header Host $host;
    proxy_cache_path /data/cache levels=1:2 keys_zone=my-cache:8m max_size=1000m inactive=600m;
    if ( $arg_test ){
        proxy_pass http://backend:backport;
    }
}

这个回答可能也不符合你的要求, 但真的需要对cache的配置进行条件化吗?
帖个更细节点的配置文件也许能更了解你的需求...

欢迎关注本人博客.

【热门文章】
【热门文章】