# Using 2 intersection of conditions for proxy\_cache\_bypass (avoiding logical if/and)

**URL:** <https://www.ruby-forum.com/t/using-2-intersection-of-conditions-for-proxy-cache-bypass-avoiding-logical-if-and/238711>\
**Category:** NGINX\
**Created:** [January 24, 2014, 9:46pm UTC](https://www.ruby-forum.com/t/using-2-intersection-of-conditions-for-proxy-cache-bypass-avoiding-logical-if-and/238711 "2014-01-24T21:46:40Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Detlef\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/58f4c7/32.png) [@Detlef\_R](https://www.ruby-forum.com/u/Detlef_R)\
**Post date:** [January 24, 2014, 9:46pm UTC](https://www.ruby-forum.com/t/using-2-intersection-of-conditions-for-proxy-cache-bypass-avoiding-logical-if-and/238711/1 "2014-01-24T21:46:40Z")

</div>

I use nginx to cache both GET and POST requests. I want to use  
proxy\_cache\_bypass to allow users to bypass the cache, but ONLY for  
GET requests. POST requests should always be cached. I tried this:

map $request\_method $is\_get {  
default: “”;  
GET “true”;  
}  
proxy\_cache\_methods POST;  
proxy\_cache\_bypass $http\_cache\_control $is\_get;

However, this bypasses the cache when either $http\_cache\_control OR  
$is\_get is set. How can I achieve to set proxy\_cache\_bypass when both  
http\_cache\_control AND $is\_get are set?

---

<div class="post-metadata">

**Author:** ![Jeroen\_O](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7c8e57/32.png) [@Jeroen\_O](https://www.ruby-forum.com/u/Jeroen_O)\
**Post date:** [January 24, 2014, 10:06pm UTC](https://www.ruby-forum.com/t/using-2-intersection-of-conditions-for-proxy-cache-bypass-avoiding-logical-if-and/238711/2 "2014-01-24T22:06:08Z")

</div>

Hello,

On Fri, Jan 24, 2014 at 9:45 PM, Jeroen O.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> However, this bypasses the cache when either $http\_cache\_control OR  
> $is\_get is set. How can I achieve to set proxy\_cache\_bypass when both  
> http\_cache\_control AND $is\_get are set?

​The logic you wish imply using a single variable in proxy\_cache\_bypass  
which is set if and only if both $http\_cache\_control and $\_is\_get are  
set.

Does the following ​work?

map $request\_method $is\_get {  
default: “”;  
GET “true”;  
}

set $bypass “true”; # Defaults to true

# Each map then deactivates the $bypass variable if one

# of the variables linked with the AND logic is empty

map $http\_cache\_control $bypass {  
default: “”;  
“”: $bypass;  
}

map $is\_get $bypass {  
default: “”;  
“”: $bypass;  
}

## proxy\_cache\_methods POST; ​​​​​proxy\_cache\_bypass $bypass;

_B. R._

---

<div class="post-metadata">

**Author:** ![Jeroen\_O](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7c8e57/32.png) [@Jeroen\_O](https://www.ruby-forum.com/u/Jeroen_O)\
**Post date:** [January 25, 2014, 3:41am UTC](https://www.ruby-forum.com/t/using-2-intersection-of-conditions-for-proxy-cache-bypass-avoiding-logical-if-and/238711/3 "2014-01-25T03:41:27Z")

</div>

On Fri, Jan 24, 2014 at 1:04 PM, B.R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Does the following work?

This looks like a fragile solution. You’re basically simulating an  
“if”, but I don’t think we should assume that nginx will resolve all  
maps in the defined order, as would be using “if”.

The nginx documentation for HttpMapModule says: “The map directive  
creates the variable, but only performs the mapping operation when  
(and if) the variable is accessed.” In your solution, $bypass is  
already set to “true” a-priori, and also defined in two maps. I doubt  
nginx will resolve those maps, in the right order, to arrive at the  
desired value of $bypass.

Maybe someone from the nginx team can comment if this is a viable  
solution?

---

<div class="post-metadata">

**Author:** ![Jeroen\_O](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7c8e57/32.png) [@Jeroen\_O](https://www.ruby-forum.com/u/Jeroen_O)\
**Post date:** [January 25, 2014, 3:58am UTC](https://www.ruby-forum.com/t/using-2-intersection-of-conditions-for-proxy-cache-bypass-avoiding-logical-if-and/238711/4 "2014-01-25T03:58:15Z")

</div>

map $http\_cache\_control$request\_method $no\_cache {  
default 0;  
~^.+GET$ 1;  
}

proxy\_cache\_methods POST;  
proxy\_cache\_bypass $no\_cache;  
proxy\_no\_cache $no\_cache;

----appa

---------- Forwarded message ----------  
From: Jeroen O. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Date: Fri, Jan 24, 2014 at 9:45 PM  
Subject: Using 2 intersection of conditions for proxy\_cache\_bypass  
(avoiding logical if/and)  
To: removed\_email\_address@domain.invalid

I use nginx to cache both GET and POST requests. I want to use  
proxy\_cache\_bypass to allow users to bypass the cache, but ONLY for  
GET requests. POST requests should always be cached. I tried this:

map $request\_method $is\_get {  
default: “”;  
GET “true”;  
}  
proxy\_cache\_methods POST;  
proxy\_cache\_bypass $http\_cache\_control $is\_get;

However, this bypasses the cache when either $http\_cache\_control OR  
$is\_get is set. How can I achieve to set proxy\_cache\_bypass when both  
http\_cache\_control AND $is\_get are set?

---

<div class="post-metadata">

**Author:** ![Jeroen\_O](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7c8e57/32.png) [@Jeroen\_O](https://www.ruby-forum.com/u/Jeroen_O)\
**Post date:** [January 25, 2014, 4:05am UTC](https://www.ruby-forum.com/t/using-2-intersection-of-conditions-for-proxy-cache-bypass-avoiding-logical-if-and/238711/5 "2014-01-25T04:05:31Z")

</div>

Hello,

On Sat, Jan 25, 2014 at 3:40 AM, Jeroen O.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> This looks like a fragile solution. You’re basically simulating an  
> “if”, but I don’t think we should assume that nginx will resolve all  
> maps in the defined order, as would be using “if”.
> 
> ​_snip_
> 
> Maybe someone from the nginx team can comment if this is a viable solution?

​You explicited clearly you wanted to avoid if/and logic in your message  
subject (one could wonder why since there appears to be no other trivial  
solution)…  
In the end, since proxy\_cache\_bypass doc clearly state that it works  
based  
on an OR logic, what you wish won’t happen magically.

With those ​conditions set, I hardly see something that won’t look  
edgy…

## ​Maybe someone else could help you better.​ Good luck,

_B. R._

---

<div class="post-metadata">

**Author:** ![Jeroen\_O](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7c8e57/32.png) [@Jeroen\_O](https://www.ruby-forum.com/u/Jeroen_O)\
**Post date:** [January 25, 2014, 2:25pm UTC](https://www.ruby-forum.com/t/using-2-intersection-of-conditions-for-proxy-cache-bypass-avoiding-logical-if-and/238711/6 "2014-01-25T14:25:56Z")

</div>

You can chain two maps to get a logical and:

map $request\_method $is\_get {  
default 0;  
GET 1;  
}

map $http\_cache\_bypass $bypass\_cache {  
default $is\_get;  
“” 0;  
}

proxy\_cache\_methods POST;  
proxy\_cache\_bypass $bypass\_cache;

# note the lack of : after default in the maps, it’s incorrect to have

it  
there like your original map did

---

<div class="post-metadata">

**Author:** ![Jeroen\_O](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7c8e57/32.png) [@Jeroen\_O](https://www.ruby-forum.com/u/Jeroen_O)\
**Post date:** [January 26, 2014, 4:27am UTC](https://www.ruby-forum.com/t/using-2-intersection-of-conditions-for-proxy-cache-bypass-avoiding-logical-if-and/238711/7 "2014-01-26T04:27:16Z")

</div>

On Sat, Jan 25, 2014 at 5:24 AM, Jonathan K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> You can chain two maps to get a logical and:

Thank you, this is precisely what I needed.

> # note the lack of : after default in the maps, it’s incorrect to have it
> 
> there like your original map did

Good catch, thanks. Appreciate it.
