# Proxy\_cache\_key + redirects

**URL:** <https://www.ruby-forum.com/t/proxy-cache-key-redirects/184519>\
**Category:** NGINX\
**Created:** [March 3, 2010, 7:49pm UTC](https://www.ruby-forum.com/t/proxy-cache-key-redirects/184519 "2010-03-03T19:49:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mayday](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ad7895/32.png) [@mayday](https://www.ruby-forum.com/u/mayday)\
**Post date:** [March 3, 2010, 7:49pm UTC](https://www.ruby-forum.com/t/proxy-cache-key-redirects/184519/1 "2010-03-03T19:49:01Z")

</div>

Hi, community.

I’ve installed nginx as proxy-caching server and now got such a problem.  
One of backend is hosting magento websites, while trying to log in into  
admin panel (url looks like http://…/admin) system want to send client  
to url http://…/index.php/admin/dashboard but for some reason it is  
already cached and i’m getting the previous page. For this setup i’m  
using such configuration:

proxy\_cache\_key $scheme$uri$is\_args$args;  
proxy\_cache\_path /var/spool/nginx/tmp/cache levels=1:2  
keys\_zone=wholepage:1024m inactive=6h max\_size=4096m;  
proxy\_cache wholepage;  
proxy\_read\_timeout 200;  
proxy\_connect\_timeout 75;  
proxy\_cache\_valid 200 1h;  
proxy\_cache\_valid 301 1h;  
proxy\_cache\_valid any 0;

location / {  
proxy\_set\_header Host $host;  
proxy\_set\_header X-Real-IP $remote\_addr;  
proxy\_set\_header X-Forwarded-For $proxy\_add\_x\_forwarded\_for;  
proxy\_pass http://…/;  
proxy\_redirect off;  
}

Of course, with such setup when i load [a.com](http://a.com) and after 5 min load [b.com](http://b.com)  
i see cached result from [a.com](http://a.com). But with this login page i can’t even  
imagine why this to different urls is caching as the same one.

Also, if i add $host in proxy\_cache\_key i’m starting to get permanent  
redirects.  
When i’m trying to load [www.a.com](http://www.a.com) backend reports 302 Location:  
[http://www.a.com](http://www.a.com) and this loop continious forever.

What i’m doing wrong and what proxy\_cache\_key i should use to get this  
working properly?

I’m using nginx/0.8.19

---

<div class="post-metadata">

**Author:** ![mayday](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ad7895/32.png) [@mayday](https://www.ruby-forum.com/u/mayday)\
**Post date:** [March 3, 2010, 8:26pm UTC](https://www.ruby-forum.com/t/proxy-cache-key-redirects/184519/2 "2010-03-03T20:26:46Z")

</div>

On Wed, 2010-03-03 at 19:49 +0100, Taras Girnyk wrote:

> Of course, with such setup when i load [a.com](http://a.com) and after 5 min load [b.com](http://b.com)  
> i see cached result from [a.com](http://a.com). But with this login page i can’t even  
> imagine why this to different urls is caching as the same one.

Unfortunately nginx site lacks sane examples.

If you happen to have Host: header based virtual servers on the same  
backend and you don’t use separate nginx server {} sections for those  
virtuals on the frontend, you want to use $host variable within  
proxy\_cache\_key.

I use  
proxy\_cache\_key $scheme://$host$request\_uri;

> Also, if i add $host in proxy\_cache\_key i’m starting to get permanent  
> redirects.

This may be the side effect of the application on the backend,  
which behaves this way when it cannot set cookies ( doesn’t receive  
it after trying to set it ). Use  
proxy\_pass\_header Set-Cookie;  
within your proxying location / {}

–  
Miroslaw “Psyborg” Jaworski  
GCS/IT d- s+:+ a C++$ UBI++++$ P+++$ L- E— W++(+++)$ N++ o+ K- w-- O-  
M- V- PS+ PE++ Y+ PGP t 5? X+ R++ !tv b++(+++) DI++ D+ G e\* h++ r+++ y?

---

<div class="post-metadata">

**Author:** ![mayday](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ad7895/32.png) [@mayday](https://www.ruby-forum.com/u/mayday)\
**Post date:** [March 3, 2010, 9:03pm UTC](https://www.ruby-forum.com/t/proxy-cache-key-redirects/184519/3 "2010-03-03T21:03:38Z")

</div>

> I use  
> proxy\_cache\_key $scheme://$host$request\_uri;

Yes, i done this to be able at least to test something.

> proxy\_pass\_header Set-Cookie;  
> within your proxying location / {}

Thank you for your reply, i just find this solution half an hour ago, i  
was trying to do this with all Cookie-commands possible which i found  
over the web, finally came to this solution and all is working fine now.

Anyway, thanks.
