查询string为空时如何禁用代理caching?

nginx我有

server { listen 1.2.3.4:80 proxy_cache_valid 200 302 5m; location / { try_files $uri @upstream; root $root; } } 

当我去http://example.com/foobar它会生成一个redirect到http://example.com/foobar http://example.com/foobar?filter_distance=50&...这是访问者依赖,所以我想不caching这个redirect。 查询string为空时,我需要绕过caching。 我有点失落,因为location /foobar将匹配两个。

我补充说

 map $request_uri $nocache { /foobar 1; } 

到http部分和

 proxy_cache_bypass $nocache; proxy_no_cache $nocache; 

到服务器部分。 这似乎是工作。

你应该像这样使用proxy_cache_bypassproxy_no_cache指令:

 set $nocache 0; if ($arg_filter_distance = "") { set $nocache 1; } proxy_cache_bypass $nocache; proxy_no_cache $nocache; 

从nginx文档定义proxy_no_cache

定义响应不会保存到caching的条件。 如果string参数中至less有一个值不为空且不等于“0”,则不会保存响应:

这里我们testing一下filter_distance GET参数是否为空。 如果是,我们将$ nocache设置为1,然后proxy_cache_bypassproxy_no_cache指令将被激活。

如果你有一个filter_type GET参数,你可以添加其他的GET参数,比如$arg_filter_type