# Proxy\_cache only if custom header is set by upstream

**URL:** <https://www.ruby-forum.com/t/proxy-cache-only-if-custom-header-is-set-by-upstream/219763>\
**Category:** NGINX\
**Created:** [May 15, 2012, 5:42pm UTC](https://www.ruby-forum.com/t/proxy-cache-only-if-custom-header-is-set-by-upstream/219763 "2012-05-15T17:42:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [May 15, 2012, 5:42pm UTC](https://www.ruby-forum.com/t/proxy-cache-only-if-custom-header-is-set-by-upstream/219763/1 "2012-05-15T17:42:54Z")

</div>

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. Any ideas or am i missing something?

cheers

Thomas Lohner

Posted at Nginx Forum:

> **[proxy\_cache only if custom header is set by upstream](https://forum.nginx.org/read.php?2,226461,226461#msg-226461)**
>
> 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...

---

<div class="post-metadata">

**Author:** ![ThomasLohner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c5a1d2/32.png) [@ThomasLohner](https://www.ruby-forum.com/u/ThomasLohner)\
**Post date:** [May 15, 2012, 5:50pm UTC](https://www.ruby-forum.com/t/proxy-cache-only-if-custom-header-is-set-by-upstream/219763/2 "2012-05-15T17:50:59Z")

</div>

Hello!

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

> set $nocache 1;
> 
> 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, [Module ngx\_http\_map\_module](http://nginx.org/r/map).

Something like this should work:

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

proxy_no_cache $nocache;

```

Maxim D.

---

<div class="post-metadata">

**Author:** ![ThomasLohner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c5a1d2/32.png) [@ThomasLohner](https://www.ruby-forum.com/u/ThomasLohner)\
**Post date:** [May 15, 2012, 5:59pm UTC](https://www.ruby-forum.com/t/proxy-cache-only-if-custom-header-is-set-by-upstream/219763/3 "2012-05-15T17:59:02Z")

</div>

On Tuesday 15 May 2012 19:42:12 ThomasLohner wrote:

> set $nocache 1;
> 
> But this doesn’t work. Any ideas or am i missing something?

The rewrite module works before the request is passed to upstream, so  
upstream  
variables is empty on rewrite stage.

You can use the map directive which is evaluated at the time of use.

wbr, Valentin V. Bartenev

---

<div class="post-metadata">

**Author:** ![ThomasLohner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c5a1d2/32.png) [@ThomasLohner](https://www.ruby-forum.com/u/ThomasLohner)\
**Post date:** [May 15, 2012, 6:25pm UTC](https://www.ruby-forum.com/t/proxy-cache-only-if-custom-header-is-set-by-upstream/219763/4 "2012-05-15T18:25:43Z")

</div>

Yeah! Thanks, maybe you should add to the wiki, that “if” is executed  
before proxy\_pass 😉

Anyways, thank you very much for this helpful answer, it works this  
way.

obviously the map has to be the other way around, so if anyone finds  
this… the correct way for caching only if upstream sets a custom header  
is:

map $upstream\_http\_myheader $nocache {  
default 1;  
1 0;  
}

proxy\_no\_cache $nocache;

Posted at Nginx Forum:

> **[Re: proxy\_cache only if custom header is set by upstream](https://forum.nginx.org/read.php?2,226461,226467#msg-226467)**
>
> 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...
