Multiple caching solutions for anonymous and logged in users with SSI on

Max nginxyz at mail.ru
Tue Mar 6 18:14:24 UTC 2012


06 марта 2012, 14:02 от "Antonio P.P. Almeida" <appa at perusio.net>:
> > Thanks Antonio.
> >
> > On a side note, if I craft a proxy cache key with cookie_sessionid and if
> > the sessionid cookie does not exist, *will nginx take it as an empty
> > value*?
> 
> Sure. Here's an example using map.
> 
> map $http_cookie $cache_id {
>     default nil; # hommage to Lisp :)
>     ~SESS[[:alnum:]]+=(?<session_id>[[:graph:]]+) $session_id;
> }
> 
> When there's no SESS cookie then $cache_id is nil.
> 
> > Can nginx do a try_files or 'if' on the existence of a cookie? This
> > will
> > help me deliver two caching strategies in the same location directive.
> 
> Sure. Example. At the http level:
> 
> map $http_cookie $cookie_exists {
>     default 0;
>     ~<regex testing cookie> 1;
> }
> 
> On the "main" cache location:
> 
> error_page 418 @other-cache-location;
> 
> if ($cookie_exists) {
>    return 418;
> }

What a load of... unnecessary directives! This does the job
much more efficiently and cleanly:

    server {

        if ($cookie_sessionid) {
            rewrite ^ /logged_in$request_uri last;
        }

        location / {
            # Non-logged in clients
            #...
        }

        location /logged_in {
            # Logged in clients

            internal;

            proxy_cache_key $cookie_sessionid$scheme$host$request_uri;

            proxy_pass http://localhost:82/; # Note the trailing slash
            #...
        }
    }

Max


More information about the nginx mailing list