My way to force cache expire

Ryan Malayter malayter at gmail.com
Thu Jun 24 19:18:43 MSD 2010


On Thu, Jun 24, 2010 at 10:04 AM, Simone fumagalli
<simone.fumagalli at contactlab.com> wrote:
> 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;
>
>  }
>
> }
>

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



More information about the nginx mailing list