收到特定请求时绕过 Nginx 缓存

收到特定请求时绕过 Nginx 缓存

我正在为我的应用程序使用 Nginx 缓存。我想绕过一个非常具体的请求字符串的缓存。我认为我可以使用类似下面的方法来实现这一点:

location / {
 if($uri ~ .*(ServerCheck).*) {set $nocache 1; }
 proxy_pass http://127.0.0.1:9001/;
 proxy_no_cache $nocache;
 proxy_cache_bypass $nocache;
 proxy_cache crownstudent;
 proxy_cache_valid 200 1y;
 proxy_cache_use_stale error timeout invalid_header updating
 http_500 http_502 http_503 http_504;
}

但是,这似乎不起作用。有什么办法可以实现这一点吗?Nginx 文档非常不清楚,而且缺乏示例。

谢谢!

答案1

$uri拥有规范化url,可以通过任何其他指令或重定向进行修改。如果你的 nginx 配置越来越长,事情可能会变得更加复杂。我更喜欢$请求uri(根据 nginx 文档:带有参数的完整原始请求 URI)。

尝试用以下情况替换您的情况:

if($request_uri ~ ServerCheck) {set $nocache 1; }

顺便说一句,你不需要捕获目标字符串服务器检查,因为 nginx 只是测试正则表达式。

您可能还想确保请求已由代理回复。我使用 add_header 指令来完成这项工作。例如:

add_header X-Proxy-Cache $upstream_cache_status;

相关内容