# My way to force cache expire

**URL:** <https://www.ruby-forum.com/t/my-way-to-force-cache-expire/190657>\
**Category:** NGINX\
**Created:** [June 24, 2010, 5:05pm UTC](https://www.ruby-forum.com/t/my-way-to-force-cache-expire/190657 "2010-06-24T17:05:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Simone\_fumagalli](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bb73d2/32.png) [@Simone\_fumagalli](https://www.ruby-forum.com/u/Simone_fumagalli)\
**Post date:** [June 24, 2010, 5:05pm UTC](https://www.ruby-forum.com/t/my-way-to-force-cache-expire/190657/1 "2010-06-24T17:05:39Z")

</div>

I’ve Nginx in front of my CMS that cache requests without cookie  
(anonymous visitors) while other requests (logged in users) are passed  
to the backend.

The conf look like this (only relevant parts) :

* * *

proxy\_cache\_path /usr/local/www/cache/myapp/html levels=1:2  
keys\_zone=MYAPP\_HTML\_CACHE:10m inactive=30m max\_size=2g;

server {

server\_name [www.myapp.com](http://www.myapp.com)  
listen 111.222.333.444:80;

proxy\_cache\_key “$scheme://$host$request\_uri”;  
proxy\_cache\_valid 200 20m;

proxy\_redirect off;  
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\_temp\_path /usr/local/tmp;

location / {

```
    # If logged in, don't cache.
    if ($http_cookie ~* "my_app_cookie" ) {
            set $do_not_cache 1;
    }

    proxy_cache_key "$scheme://$host$request_uri$do_not_cache";
    proxy_cache MYAPP_HTML_CACHE;

```

proxy\_pass http://ALL\_backend;

}

}

* * *

Everything works but now I’m looking for a way to delete cached pages  
when they are updated from the backend  
I thought I could make an http get from my backend with a particular  
cookie (backend\_cookie) to the URL of the page I want to update.  
The backend is configured in a way so that HTTP requests pass by the  
proxy and are treated like external request.

So, do you think this setup can work ?

I’ve added only the 3 line with # at the beginning

* * *

proxy\_cache\_path /usr/local/www/cache/myapp/html levels=1:2  
keys\_zone=MYAPP\_HTML\_CACHE:10m inactive=30m max\_size=2g;

server {

server\_name [www.myapp.com](http://www.myapp.com)  
listen 111.222.333.444:80;

proxy\_cache\_key “$scheme://$host$request\_uri”;  
proxy\_cache\_valid 200 20m;

proxy\_redirect off;  
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\_temp\_path /usr/local/tmp;

location / {

```
    # Do not cache for logged user
    if ($http_cookie ~* "my_app_cookie" ) {
            set $do_not_cache 1;
    }

```

# if ($http\_cookie ~\* “my\_backend\_cookie” ) {

# proxy\_cache\_valid 200 0m;

# }

proxy\_cache\_key “$scheme://$host$request\_uri$do\_not\_cache”;  
proxy\_cache MYAPP\_HTML\_CACHE;  
proxy\_pass http://ALL\_backend;

}

}

* * *

Let me know

–  
Simone F.

---

<div class="post-metadata">

**Author:** ![Simone\_fumagalli](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bb73d2/32.png) [@Simone\_fumagalli](https://www.ruby-forum.com/u/Simone_fumagalli)\
**Post date:** [June 24, 2010, 5:19pm UTC](https://www.ruby-forum.com/t/my-way-to-force-cache-expire/190657/2 "2010-06-24T17:19:42Z")

</div>

On Thu, Jun 24, 2010 at 10:04 AM, Simone fumagalli  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Â server\_name [www.myapp.com](http://www.myapp.com)  
> Â proxy\_temp\_path /usr/local/tmp;  
> Â Â Â Â proxy\_pass http://ALL\_backend;
> 
> Â }
> 
> }

I don’t think this configuration is doing what you want. I think all  
logged-in users will get the same cached data, since the  
proxy\_cache\_key will be the same for every logged in user.

One way might be to use something like the following (assuming  
proxy\_cache will accept a variable - I haven’t tested):

location / {  
# If logged in, don’t cache.  
set $mycache MYAPP\_HTML\_CACHE;  
if ($http\_cookie ~\* “my\_app\_cookie” ) {  
set $mycache off;  
}  
proxy\_cache\_key “$scheme://$host$request\_uri$do\_not\_cache”;  
proxy\_cache $mycache;  
proxy\_pass http://ALL\_backend;

}

–  
RPM

---

<div class="post-metadata">

**Author:** ![Simone\_fumagalli](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bb73d2/32.png) [@Simone\_fumagalli](https://www.ruby-forum.com/u/Simone_fumagalli)\
**Post date:** [June 24, 2010, 5:23pm UTC](https://www.ruby-forum.com/t/my-way-to-force-cache-expire/190657/3 "2010-06-24T17:23:48Z")

</div>

On 06/24/2010 05:18 PM, Ryan M. wrote:

> I don’t think this configuration is doing what you want.

It does. It’s in production now and it works perfectly.  
I have doubts about the second configuration

–  
Simone F.

---

<div class="post-metadata">

**Author:** ![Simone\_fumagalli](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bb73d2/32.png) [@Simone\_fumagalli](https://www.ruby-forum.com/u/Simone_fumagalli)\
**Post date:** [June 24, 2010, 5:44pm UTC](https://www.ruby-forum.com/t/my-way-to-force-cache-expire/190657/4 "2010-06-24T17:44:17Z")

</div>

Hi,

> It does. It’s in production now and it works perfectly.  
> I have doubts about the second configuration

It does, but only because your backend doesn’t set cache headers for  
logged-in users, otherwise all your users would see the same page.

You should really use “proxy\_no\_cache” for such things:  
proxy\_no\_cache $cookie\_my\_app\_cookie;

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

---

<div class="post-metadata">

**Author:** ![Simone\_fumagalli](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bb73d2/32.png) [@Simone\_fumagalli](https://www.ruby-forum.com/u/Simone_fumagalli)\
**Post date:** [June 24, 2010, 5:46pm UTC](https://www.ruby-forum.com/t/my-way-to-force-cache-expire/190657/5 "2010-06-24T17:46:03Z")

</div>

Hi,

> Everything works but now I’m looking for a way to delete cached pages when  
> they are updated from the backend  
> I thought I could make an http get from my backend with a particular  
> cookie (backend\_cookie) to the URL of the page I want to update.  
> The backend is configured in a way so that HTTP requests pass by the proxy  
> and are treated like external request.
> 
> So, do you think this setup can work ?

Please check ngx\_cache\_purge module:

> **[FRiCKLE Labs / nginx / ngx\_cache\_purge](http://labs.frickle.com/nginx_ngx_cache_purge/)**

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

---

<div class="post-metadata">

**Author:** ![Simone\_fumagalli](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bb73d2/32.png) [@Simone\_fumagalli](https://www.ruby-forum.com/u/Simone_fumagalli)\
**Post date:** [June 24, 2010, 6:10pm UTC](https://www.ruby-forum.com/t/my-way-to-force-cache-expire/190657/6 "2010-06-24T18:10:04Z")

</div>

On 06/24/2010 05:43 PM, Piotr S. wrote:

> It does, but only because your backend doesn’t set cache headers for  
> logged-in users, otherwise all your users would see the same page.

I forgot to mention that I set X-Accel-Expires header to 0 for backend  
response for logged in users.

–  
Simone F.
