# Proxy\_cache\_key with variables

**URL:** <https://www.ruby-forum.com/t/proxy-cache-key-with-variables/242601>\
**Category:** NGINX\
**Created:** [July 6, 2015, 3:50pm UTC](https://www.ruby-forum.com/t/proxy-cache-key-with-variables/242601 "2015-07-06T15:50:15Z")\
**Posts on this page:** 3\
**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:** [July 6, 2015, 3:50pm UTC](https://www.ruby-forum.com/t/proxy-cache-key-with-variables/242601/1 "2015-07-06T15:50:15Z")

</div>

I’ve just created a configuration composed of two servers.  
Both just forwards traffic from different ports (and protocols) to  
different  
servers, and keep responses in cache. It works nice.

For both servers “GET /” request is sent, in the response there is  
proper  
resource. Thus (to distinguish responses in the cache) I added for both  
cache\_key:

proxy\_cache\_key “$upstream\_addr\*$request”;

But it doesn’t work. I found that upstream states are not available and  
thus  
“proxied” address is not known when cache key is computed. And “proxied”  
address is computed only when additional stream must be used…

Is there any way to get proper key? Or any other way to keep all copies  
in  
the cache?

I plan to add “load balancing” module, which would be able to forward  
request on (multiple) source addresses to multiple proxied addresses,  
with  
caching… Will it work properly?

Posted at Nginx Forum:

> **[proxy\_cache\_key with variables](https://forum.nginx.org/read.php?2,260086,260086#msg-260086)**
>
> I've just created a configuration composed of two servers. Both just forwards traffic from different ports (and protocols) to different servers, and keep responses in cache. It works nice. For both servers "GET /" request is sent, in the...

---

<div class="post-metadata">

**Author:** ![sz\_g](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9f8e36/32.png) [@sz\_g](https://www.ruby-forum.com/u/sz_g)\
**Post date:** [July 6, 2015, 4:06pm UTC](https://www.ruby-forum.com/t/proxy-cache-key-with-variables/242601/2 "2015-07-06T16:06:02Z")

</div>

Hi,

> > proxy\_cache\_key “$upstream\_addr\*$request”;  
> > I am not sure that you are using the correct format for proxy\_cache\_key  
> > directive.  
> > my I know what is the wildcard char “\*” doing out there, between  
> > $upstream\_addr and $request?

refer to nginx directive example:  
proxy\_cache\_key “$host$request\_uri $cookie\_user”;  
or by default is  
proxy\_cache\_key $scheme$proxy\_host$uri$is\_args$args;

> > I plan to add “load balancing” module, which would be able to forward  
> > request on (multiple) source addresses to multiple proxied addresses,  
> > with  
> > caching.. Will it work properly?  
> > if your nginx is acting as load balancer, I guess better add load  
> > balancing  
> > module.

Posted at Nginx Forum:  
[http://forum.nginx.org/read.php?2,260086,260088#msg-260088](http://forum.nginx.org/read.php?2,260086,260088#msg-260088)

---

<div class="post-metadata">

**Author:** ![sz\_g](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9f8e36/32.png) [@sz\_g](https://www.ruby-forum.com/u/sz_g)\
**Post date:** [July 6, 2015, 5:44pm UTC](https://www.ruby-forum.com/t/proxy-cache-key-with-variables/242601/3 "2015-07-06T17:44:46Z")

</div>

> proxy\_cache\_key "$upstream\_addr\*$request”;  
> At the time when this expression is evaluated, $upstream\_addr is still  
> unknown  
> since no upstream connection is made. Obviously, if a key is found in  
> cache,  
> there will be no upstream connection at all. So it’s not a good idea to  
> use  
> $upstream\_addr in key definition. Instead, you can use the parts of  
> request  
> which define the upstream address.

> caching.. Will it work properly?
> 
> Posted at Nginx Forum:  
> [http://forum.nginx.org/read.php?2,260086,260086#msg-260086](http://forum.nginx.org/read.php?2,260086,260086#msg-260086)
> 
> * * *
> 
> nginx mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [mailman.nginx.org archive](http://mailman.nginx.org/mailman/listinfo/nginx)

–  
Roman A.
