Conditionally set proxy_cache_use_stale parameters in Nginx

qgelzfjb  于 2022-12-11  发布在  Nginx
关注(0)|答案(1)|浏览(152)

I need to active/inactive 'updating' params of proxy_cache_use_stale directive based on a cookie value in Nginx.
This is the normal config

proxy_cache_use_stale error updating timeout http_500;

I change the config to the following:

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;
}

When I validate the new config through nginx -t the following error raised.
nginx: [emerg] "proxy_cache_use_stale" directive is not allowed ...
How can I do that?

v09wglhw

v09wglhw1#

I used 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
    }
}

相关问题