# Issue with proxy\_cache\_key after rewrite

**URL:** <https://www.ruby-forum.com/t/issue-with-proxy-cache-key-after-rewrite/200919>\
**Category:** NGINX\
**Created:** [December 24, 2010, 11:58pm UTC](https://www.ruby-forum.com/t/issue-with-proxy-cache-key-after-rewrite/200919 "2010-12-24T23:58:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nikuSESS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f17d59/32.png) [@nikuSESS](https://www.ruby-forum.com/u/nikuSESS)\
**Post date:** [December 24, 2010, 11:58pm UTC](https://www.ruby-forum.com/t/issue-with-proxy-cache-key-after-rewrite/200919/1 "2010-12-24T23:58:29Z")

</div>

Hi,

i’ve run a few tests with the nginx proxy module. I would like to have  
my content cached with the “$scheme$host$request\_uri” key, like  
suggested  
on the wiki page[1]. So i’ve added the following:  
proxy\_cache\_key “$scheme$host$request\_uri”;

into the http {} block. After the test i’ve checked the keys which are  
used  
in the cached files and noticed that the key was  
httpmy\_vhost/original\_request\_uri

but i’ve a perl module which does a internal\_redirect which rewrites  
some  
paths like /olddir/ to /.

Now we have the following request:  
http//my\_vhost/olddir/documents/test.pdf

so i thought the key should be  
httpmy\_vhost/documents/test.pdf

but it was  
httpmy\_vhost/olddir/documents/test.pdf

when i remove the proxy\_cache\_key from the config i get the following  
key:  
http://interal\_host:internal\_port/documents/test.pdf

so something here seems to be “strange”. Because the default key is:  
default: $scheme$proxy\_host$request\_uri;

and now we see the following differences:  
first: $scheme seems to be http when beeing used, but http:// when  
default.  
second: the $request\_uri is not rewritten when defined as key, but it  
seems  
to be rewritten by default.

Seems like a bug to me…?

Regards,  
Sven

---

<div class="post-metadata">

**Author:** ![Sven\_SDarkmanS\_Mich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e99b99/32.png) [@Sven\_SDarkmanS\_Mich](https://www.ruby-forum.com/u/Sven_SDarkmanS_Mich)\
**Post date:** [December 25, 2010, 2:31am UTC](https://www.ruby-forum.com/t/issue-with-proxy-cache-key-after-rewrite/200919/2 "2010-12-25T02:31:04Z")

</div>

Hi,

> So i’ve added the following:  
> proxy\_cache\_key “$scheme$host$request\_uri”;

Try using “$scheme$host$uri$is\_args$args” 😉

> when i remove the proxy\_cache\_key from the config i get the following key:  
> http://interal\_host:internal\_port/documents/test.pdf
> 
> so something here seems to be “strange”. Because the default key is:  
> default: $scheme$proxy\_host$request\_uri;

No, default key isn’t calculated from variables…  
It’s something like: “${proxy\_scheme}://$proxy\_host$uri$is\_args$args”

Also, “$proxy\_host” returns whatever you’ve got in your “proxy\_pass”  
directive, so in case you’ve got “proxy\_pass [http://127.0.0.1:8000](http://127.0.0.1:8000)” it  
will  
return “127.0.0.1:8000”.

> first: $scheme seems to be http when beeing used, but http:// when  
> default.

See above.

> second: the $request\_uri is not rewritten when defined as key, but it  
> seems  
> to be rewritten by default.

“$request\_uri” isn’t used in the default cache key.

Best regards,  
Piotr S. \< [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) \>

---

<div class="post-metadata">

**Author:** ![Sven\_SDarkmanS\_Mich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e99b99/32.png) [@Sven\_SDarkmanS\_Mich](https://www.ruby-forum.com/u/Sven_SDarkmanS_Mich)\
**Post date:** [December 25, 2010, 2:42am UTC](https://www.ruby-forum.com/t/issue-with-proxy-cache-key-after-rewrite/200919/3 "2010-12-25T02:42:20Z")

</div>

Hi,

On 25/12/2010 03:29, Piotr S. wrote:

> > So i’ve added the following:  
> > proxy\_cache\_key “$scheme$host$request\_uri”;
> 
> Try using “$scheme$host$uri$is\_args$args” 😉  
> Probably putting ‘://’ or something else that won’t be the first  
> character of a URL (e.g. ‘#’) between $scheme and $host is a good idea,  
> just in case you have two hosts that only differ by an initial ‘s’ -

e.g. [mysite.com](http://mysite.com) and [smysite.com](http://smysite.com)

https + [mysite.com](http://mysite.com) = http + [smysite.com](http://smysite.com)

In most situations this will be fine, but depending on the setup, it  
could potentially be a source of bugs or a security hole.

Marcus.

---

<div class="post-metadata">

**Author:** ![Sven\_SDarkmanS\_Mich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e99b99/32.png) [@Sven\_SDarkmanS\_Mich](https://www.ruby-forum.com/u/Sven_SDarkmanS_Mich)\
**Post date:** [December 25, 2010, 2:44pm UTC](https://www.ruby-forum.com/t/issue-with-proxy-cache-key-after-rewrite/200919/4 "2010-12-25T14:44:14Z")

</div>

Hi,

Am 25.12.2010 04:48, schrieb Maxim D.:

> [1] [Модуль ngx\_http\_proxy\_module](http://sysoev.ru/nginx/docs/http/ngx_http_proxy_module.html#proxy_cache_key)  
> Aww, okay. So its a wiki documentation Bug. 🙂 Thanks a lot, works now.

Regards,  
Sven

---

<div class="post-metadata">

**Author:** ![Sven\_SDarkmanS\_Mich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e99b99/32.png) [@Sven\_SDarkmanS\_Mich](https://www.ruby-forum.com/u/Sven_SDarkmanS_Mich)\
**Post date:** [December 25, 2010, 4:49am UTC](https://www.ruby-forum.com/t/issue-with-proxy-cache-key-after-rewrite/200919/5 "2010-12-25T04:49:28Z")

</div>

Hello!

On Fri, Dec 24, 2010 at 11:57:30PM +0100, Sven ‘Darkman’ Michels wrote:

> httpmy\_vhost/olddir/documents/test.pdf  
> to be rewritten by default.
> 
> Seems like a bug to me…?

Default listed in wiki/docs isn’t what actually used by default as  
proxy\_cache\_key. In russian docs[1] you may find something like

```
By default value is similar to
proxy_cache_key $scheme$proxy_host$uri$is_args$args;

```

Word “similar” is key one. Actual value can’t be expressed with  
variables. It’s something like $proxy\_url - but there are no such  
variable.

[1]

> **[Модуль ngx\_http\_proxy\_module](http://nginx.org/ru/docs/http/ngx_http_proxy_module.html)**

Maxim D.
