My way to force cache expire

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
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
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.

On Thu, Jun 24, 2010 at 10:04 AM, Simone fumagalli
[email protected] wrote:

 server_name 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

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.

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. < [email protected] >

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:
http://labs.frickle.com/nginx_ngx_cache_purge/

Best regards,
Piotr S. < [email protected] >

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.