简体   繁体   中英

Conditionally set proxy_cache_use_stale parameters in Nginx

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM