# How to Cache dynamic content using Nginx when sessions are involved?How to Cache dynamic content usi

**URL:** <https://www.ruby-forum.com/t/how-to-cache-dynamic-content-using-nginx-when-sessions-are-involved-how-to-cache-dynamic-content-usi/217258>\
**Category:** NGINX\
**Created:** [February 26, 2012, 6:13am UTC](https://www.ruby-forum.com/t/how-to-cache-dynamic-content-using-nginx-when-sessions-are-involved-how-to-cache-dynamic-content-usi/217258 "2012-02-26T06:13:56Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [February 26, 2012, 6:13am UTC](https://www.ruby-forum.com/t/how-to-cache-dynamic-content-using-nginx-when-sessions-are-involved-how-to-cache-dynamic-content-usi/217258/1 "2012-02-26T06:13:56Z")

</div>

This site explains how to create static files from dynamic content using  
Nginx.

> **[Ossdl](https://ossdl.de/)**
>
> Blog

My question is this: can I achieve the same if login sessions are  
involved. ie. when I want to serve content to only registered users and  
not otherwise. So how to overcome sessions when it comes to caching and  
finally to use the cache next time for another session?

Detailed scenario:

The goal of my website is to serve content to only registered users.

There are plenty of users logged in, each having different session IDs.

A php page queries the DB and finds “XYZ” that user “A” wants and  
generates HTML output.

Now if user “B” (with a different session ID) after sometime wants the  
same “XYZ”, how to make Nginx to deliver from cache without making the  
php page to query the db again.

Has anybody done this before? Thanks in advance.

Posted at Nginx Forum:

> **[How to Cache dynamic content using Nginx when sessions are involved?How to...](https://forum.nginx.org/read.php?2,222987,222987#msg-222987)**
>
> This site explains how to create static files from dynamic content using Nginx. http://mark.ossdl.de/2009/07/nginx-to-create-static-files-from-dynamic-content/ My question is this: can I achieve the same if login sessions are involved. ie. when I...

---

<div class="post-metadata">

**Author:** ![tinkutalking](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7feea3/32.png) [@tinkutalking](https://www.ruby-forum.com/u/tinkutalking)\
**Post date:** [February 26, 2012, 3:24pm UTC](https://www.ruby-forum.com/t/how-to-cache-dynamic-content-using-nginx-when-sessions-are-involved-how-to-cache-dynamic-content-usi/217258/2 "2012-02-26T15:24:07Z")

</div>

On 26 Fev 2012 06h13 CET, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> making the php page to query the db again.  
> You mean you want registered users to be served the same page  
> regardless of their respective session ID?

> Has anybody done this before? Thanks in advance.

If that’s the case then it’s simple. From the given example:

At the http level:

map $uri $cache\_uri {  
default $uri;  
/ index.html;  
}

At the server level:

location ~ ^/200[0-8]/[01][0-9]/ {  
root /tmp/nginx/blog/fetch;  
expires 30d;  
error\_page 403 404 = /fetch$uri;  
}

location ^~ /fetch/ {  
internal;  
proxy\_pass [http://127.0.0.1:80](http://127.0.0.1:80);  
proxy\_store /tmp/nginx/blog$cache\_uri;  
proxy\_store\_access user:rw group:rw all:r;  
}

— appa
