【问题标题】:Conditionally set proxy_cache_use_stale parameters in Nginx在 Nginx 中有条件地设置 proxy_cache_use_stale 参数
【发布时间】:2022-12-11 06:06:55
【问题描述】:
我需要激活/不激活 '更新' 的参数proxy_cache_use_stale基于 Nginx 中的 cookie 值的指令。
这是正常配置
proxy_cache_use_stale error updating timeout http_500;
我将配置更改为以下内容:
if ($cookie_req = 1){
proxy_cache_use_stale error updating timeout http_500;
}
if ($cookie_req = 2){
proxy_cache_use_stale error timeout http_500;
}
当我通过验证新配置时nginx -t出现以下错误。
nginx:[emerg] 不允许使用“proxy_cache_use_stale”指令......
我怎样才能做到这一点?
【问题讨论】:
标签:
nginx
caching
openresty
【解决方案1】:
我使用了 access_by_lua_block
location @no_updating {
proxy_pass http://upstream;
proxy_cache_use_stale error updating timeout http_500;
}
location @default {
proxy_pass http://upstream;
proxy_cache_use_stale error timeout http_500;
}
location / {
access_by_lua_block {
local no_updating = ngx.var.cookie_req
if no_updating == "2" then
ngx.exec("@no_updating")
else
ngx.exec("@default")
end
}
}