My way to force cache expire

Simone fumagalli simone.fumagalli at contactlab.com
Thu Jun 24 19:04:10 MSD 2010


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 Fumagalli



More information about the nginx mailing list