proxy_cache only if custom header is set by upstream

Maxim Dounin mdounin at mdounin.ru
Tue May 15 15:50:21 UTC 2012


Hello!

On Tue, May 15, 2012 at 11:42:12AM -0400, ThomasLohner wrote:

> Hi,
> 
> i know how to *prevent* caching if custom headers are set by upstream
> with proxy_cache_bypass and proxy_no_cache.
> 
> This time i'd like to do the opposite, i want the response to be cached
> *only* if a custom header is present.
> 
> I tried something like:
> 
> set $nocache 1;
> 
> proxy_no_cache $nocache;
> 
> ...
> 
> if ($upstream_http_myheader = 1) {
> 
>   set $nocache 0;
> 
> }
> 
> But this doesn't work.

And it's not expected to, as "if" directives, as well as other 
rewrite module directives, are executed before request goes to 
upstream and hence $upstream_http_myheader isn't known.

> Any ideas or am i missing something?

Use map instead, http://nginx.org/r/map.

Something like this should work:

    map $upstream_http_myheader $nocache {
        default 0;
        1       1;
    }

    proxy_no_cache $nocache;

Maxim Dounin



More information about the nginx mailing list