# Map is not matching correctly against upstream custom header

**URL:** <https://www.ruby-forum.com/t/map-is-not-matching-correctly-against-upstream-custom-header/244718>\
**Category:** NGINX\
**Created:** [June 2, 2016, 7:51pm UTC](https://www.ruby-forum.com/t/map-is-not-matching-correctly-against-upstream-custom-header/244718 "2016-06-02T19:51:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sadaf\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@Sadaf\_N](https://www.ruby-forum.com/u/Sadaf_N)\
**Post date:** [June 2, 2016, 7:51pm UTC](https://www.ruby-forum.com/t/map-is-not-matching-correctly-against-upstream-custom-header/244718/1 "2016-06-02T19:51:05Z")

</div>

Hi, I’m trying to configure my proxy cache settings based in a response  
custom header, using proxy\_no\_cache and proxy\_cache\_bypass directives.

First question: is it possible at all?  
Second question: If it is, why this map is always hitting the default  
value?

map $sent\_http\_x\_my\_custom\_header $no\_cache {  
default 0;  
1 0;  
true 0;  
false 1;  
“~\*false” 1;  
}

```
....

```

location ~ ^/ {  
…  
proxy\_no\_cache $no\_cache;^M  
proxy\_cache\_bypass $no\_cache;^M  
}

I’ve already tried the map with $sent\_http\_x\_my\_custom\_header,  
$upstream\_http\_x\_my\_custom\_header and $http\_x\_my\_custom\_header. It’s  
always the same result.

obs: I’ve already tried with if but if is resolved in request time so it  
didn’t work.

Thank you in advance.

---

<div class="post-metadata">

**Author:** ![aretche](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/848f3c/32.png) [@aretche](https://www.ruby-forum.com/u/aretche)\
**Post date:** [June 2, 2016, 8:27pm UTC](https://www.ruby-forum.com/t/map-is-not-matching-correctly-against-upstream-custom-header/244718/2 "2016-06-02T20:27:00Z")

</div>

Hello!

On Thu, Jun 02, 2016 at 07:51:06PM +0200, Gabriel Arrais wrote:

> ```
> 1 0;
> proxy_cache_bypass $no_cache;^M
> 
> ```
> 
> }
> 
> I’ve already tried the map with $sent\_http\_x\_my\_custom\_header,  
> $upstream\_http\_x\_my\_custom\_header and $http\_x\_my\_custom\_header. It’s  
> always the same result.
> 
> obs: I’ve already tried with if but if is resolved in request time so it  
> didn’t work.

The problem is that you are trying to lookup response headers when  
there is no response yet. Both “if” and “proxy\_cache\_bypass” are  
checked before the request is sent to a backend, and hence they  
can’t do anything good.

Additionally, map{} results are always cached, and when you try to  
lookup it again via “proxy\_no\_cache” it just return a previously  
cached value (the one computed when there were no response yet).

Consider removing “proxy\_cache\_bypass” from your cofiguration.  
Just

```
map $upstream_http_x_my_custom_header $no_cache {
    ...
}

proxy_no_cache $no_cache;

```

is expected to work fine.

–  
Maxim D.

> **[nginx](http://nginx.org/)**

---

<div class="post-metadata">

**Author:** ![aretche](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/848f3c/32.png) [@aretche](https://www.ruby-forum.com/u/aretche)\
**Post date:** [June 2, 2016, 9:12pm UTC](https://www.ruby-forum.com/t/map-is-not-matching-correctly-against-upstream-custom-header/244718/3 "2016-06-02T21:12:58Z")

</div>

Maxim D. wrote in post #1183769:

> Hello!
> 
> On Thu, Jun 02, 2016 at 07:51:06PM +0200, Gabriel Arrais wrote:
> 
> > ```
> > 1 0;
> > proxy_cache_bypass $no_cache;^M
> > 
> > ```
> > 
> > }
> > 
> > I’ve already tried the map with $sent\_http\_x\_my\_custom\_header,  
> > $upstream\_http\_x\_my\_custom\_header and $http\_x\_my\_custom\_header. It’s  
> > always the same result.
> > 
> > obs: I’ve already tried with if but if is resolved in request time so it  
> > didn’t work.
> 
> The problem is that you are trying to lookup response headers when  
> there is no response yet. Both “if” and “proxy\_cache\_bypass” are  
> checked before the request is sent to a backend, and hence they  
> can’t do anything good.
> 
> Additionally, map{} results are always cached, and when you try to  
> lookup it again via “proxy\_no\_cache” it just return a previously  
> cached value (the one computed when there were no response yet).
> 
> Consider removing “proxy\_cache\_bypass” from your cofiguration.  
> Just
> 
> ```
> map $upstream_http_x_my_custom_header $no_cache {
> ...
> }
> 
> proxy_no_cache $no_cache;
> 
> ```
> 
> is expected to work fine.
> 
> –  
> Maxim D.  
> [http://nginx.org/](http://nginx.org/)

Hello Maxim!

Thank you very much for the response, I think that I can’t remove the  
“proxy\_cache\_bypass” from my configuration because other mecanisms are  
using this directive, in other situations..

I will try to use two different variables for proxy\_no\_cache and  
proxy\_cache\_bypass.

Again, thank you!

---

<div class="post-metadata">

**Author:** ![rdn](https://www.ruby-forum.com/user_avatar/www.ruby-forum.com/rdn/32/2374_2.png) [@rdn](https://www.ruby-forum.com/u/rdn)\
**Post date:** [April 1, 2020, 9:14pm UTC](https://www.ruby-forum.com/t/map-is-not-matching-correctly-against-upstream-custom-header/244718/4 "2020-04-01T21:14:51Z")

</div>

How did you solve this issue?  
I have the same scenario, unresolved ☹
