# Session cache

**URL:** <https://www.ruby-forum.com/t/session-cache/237820>\
**Category:** NGINX\
**Created:** [December 2, 2013, 1:48pm UTC](https://www.ruby-forum.com/t/session-cache/237820 "2013-12-02T13:48:22Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![musicdenotation](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ea666f/32.png) [@musicdenotation](https://www.ruby-forum.com/u/musicdenotation)\
**Post date:** [December 2, 2013, 1:48pm UTC](https://www.ruby-forum.com/t/session-cache/237820/1 "2013-12-02T13:48:22Z")

</div>

Hi, could anybody say, may i cache dynamic content for sessions isolate  
from eache other ?  
I have very hard sql, but it is show some results which very variative  
for  
users, but some times users call this script many time in small period  
of  
time, so i wana cache the results in small period for each  
users-session.  
Is it possible in nginx.  
Nginx work as proxy\_pass http://application\_server  
And cache some static for spesific locations.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 2, 2013, 6:17pm UTC](https://www.ruby-forum.com/t/session-cache/237820/2 "2013-12-02T18:17:13Z")

</div>

On Mon, Dec 02, 2013 at 03:47:42PM +0300, Дмитрий Лабазнов wrote:

Hi there,

> Hi, could anybody say, may i cache dynamic content for sessions isolate  
> from eache other ?

[http://nginx.org/r/proxy\_cache\_key](http://nginx.org/r/proxy_cache_key)

Include something unique per session in the key.

## f

Francis D. removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 2, 2013, 6:18pm UTC](https://www.ruby-forum.com/t/session-cache/237820/3 "2013-12-02T18:18:58Z")

</div>

Could you plz some example ?

2013/12/2 Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 2, 2013, 6:30pm UTC](https://www.ruby-forum.com/t/session-cache/237820/4 "2013-12-02T18:30:40Z")

</div>

> Hi there,

> Could you plz some example ?

> What is a session?

> If it is a cookie called “user”, then something like

> proxy\_cache\_key “$host$request\_uri $cookie\_user”;

> may work for you.  
> yes cookie, what does it mean called “user” ?  
> Users have a cookie session, and this strind do not work =(

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 2, 2013, 6:21pm UTC](https://www.ruby-forum.com/t/session-cache/237820/5 "2013-12-02T18:21:54Z")

</div>

On Mon, Dec 02, 2013 at 09:18:06PM +0400, Дмитрий Лабазнов wrote:

Hi there,

> Could you plz some example ?

What is a session?

If it is a cookie called “user”, then something like

proxy\_cache\_key “$host$request\_uri $cookie\_user”;

may work for you.

## f

Francis D. removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 2, 2013, 7:54pm UTC](https://www.ruby-forum.com/t/session-cache/237820/6 "2013-12-02T19:54:14Z")

</div>

— Original message —  
From: “Francis D.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Date: 2 December 2013, 19:21:29

> proxy\_cache\_key “$host$request\_uri $cookie\_user”;  
> Or better: proxy\_cache\_key “$host$request\_uri$cookie\_user$remote\_addr”;

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 2, 2013, 6:40pm UTC](https://www.ruby-forum.com/t/session-cache/237820/7 "2013-12-02T18:40:36Z")

</div>

On Mon, Dec 02, 2013 at 09:29:53PM +0400, Дмитрий Лабазнов wrote:

Hi there,

> > If it is a cookie called “user”, then something like
> 
> > proxy\_cache\_key “$host$request\_uri $cookie\_user”;

> yes cookie, what does it mean called “user” ?  
> Users have a cookie session, and this strind do not work =(

[http://nginx.org/en/docs/http/ngx\_http\_core\_module.html#variables](http://nginx.org/en/docs/http/ngx_http_core_module.html#variables)

or maybe

[http://nginx.org/ru/docs/http/ngx\_http\_core\_module.html#variables](http://nginx.org/ru/docs/http/ngx_http_core_module.html#variables)

If the cookie name is “session”, then the nginx variable is  
$cookie\_session.

## f

Francis D. removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 3, 2013, 7:58am UTC](https://www.ruby-forum.com/t/session-cache/237820/8 "2013-12-03T07:58:02Z")

</div>

On Mon, Dec 02, 2013 at 09:29:53PM +0400, wrote:

> Hi there,

> > If it is a cookie called “user”, then something like
> 
> > proxy\_cache\_key “$host$request\_uri $cookie\_user”;

> yes cookie, what does it mean called “user” ?  
> Users have a cookie session, and this strind do not work =(

> [Module ngx\_http\_core\_module](http://nginx.org/en/docs/http/ngx_http_core_module.html#variables)

> or maybe

> [Модуль ngx\_http\_core\_module](http://nginx.org/ru/docs/http/ngx_http_core_module.html#variables)

> If the cookie name is “session”, then the nginx variable is  
> $cookie\_session.

THX!!! Its work!  
location ~\* ^/.+.(php)$ {  
proxy\_cache\_methods GET HEAD POST;  
proxy\_ignore\_headers Cache-Control Expires;  
proxy\_cache\_key “$host$request\_uri $cookie\_PHPSESSID”;  
proxy\_pass [http://localhost:8080](http://localhost:8080);  
proxy\_temp\_path /tmp/nginx/tmp;  
proxy\_cache one;  
proxy\_cache\_valid 200 304 1m;  
expires 1m;  
}  
here i cache all php dynavics, cous it is from test stand. But it is  
work,  
dynamic for various browsers cached  
in conformity with they cookies. Thx a lot!  
It is strange, this easy example i couldnt google for my case and  
situation.

2013/12/2 Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
