根据GET查询更改proxy_cache_valid值

我目前正试图设置一个nginxcaching,应该caching一个位置很长一段时间,但如果查询参数只存在几分钟。

所以基本上:

http://example.com/mypath -> long cache http://example.com/mypath?param=1 -> short cache 

我目前的configuration看起来像这样:

 location "~^(/mypath)" { proxy_cache example.com_my_cache_http; proxy_cache_valid 404 15m; ... } 

我不能使用不同的位置,因为位置不能匹配查询参数,我曾尝试使用if (我知道这是不好的做法)

 if ($args ~ param) { proxy_cache example.com_my_cache_http; proxy_cache_valid 404 15m; } else { proxy_cache example.com_my_cache_http; proxy_cache_valid 404 2d; } 

这导致:

 Testing nginx configuration: nginx: [emerg] "proxy_cache" directive is not allowed here 

我也尝试使用variables:

  set $time "1h"; if ($args ~ param) { set $time "2m"; } proxy_cache example.com_my_cache_http; proxy_cache_valid 404 $time; 

这导致:

 Testing nginx configuration: nginx: [emerg] invalid time value "$time" 

我真的用完了任何帮助或暗示赞赏。

我的第一个想法是使用map

 map $arg_param $cache_valid_404 { "" 2d; 1 15m; } 

然后你设定:

 proxy_cache_valid 404 $cache_valid_404;