fastcgi_cache_key with cookie

brianmercer nginx-forum at nginx.us
Wed Dec 28 04:15:28 UTC 2011


António P. P. Almeida Wrote:
-------------------------------------------------------
> On 27 Dez 2011 17h01 WET, nginx-forum at nginx.us
> wrote:
> 
> > zenw Wrote:
> >
> --------------------------------------------------
> -----
> >> I'm trying to cache per user Drupal using this
> >> config :
> >>
> >> fastcgi_ignore_headers Cache-Control Expires;
> >> #tried '__utma' 'Set-Cookie'
> >> fastcgi_cache drupal;
> >> fastcgi_cache_key
> >>
> $http_cookie$request_method$scheme$host$request_ur
> >> i;
> >> fastcgi_cache_methods GET HEAD;
> >> fastcgi_cache_valid any 1s;
> >> fastcgi_cache_use_stale error timeout
> >> invalid_header updating http_500;     
> >> fastcgi_max_temp_file_size 2M;
> >> add_header X-Micro-Cache
> $upstream_cache_status;
> >>
> >> I can get this working without google
> analytics,
> >> but when I enable google analytics it adds
> several
> >> cookies __utma __utmb __utmc __utmz.  Nginx
> looks
> >> at $http_cookie and sees that its different so
> it
> >> passes it to the backend.  Preferably I would
> like
> >> to just use the cache key as $cookie_SESS which
> is
> >> the drupal session cookie, however the full
> cookie
> >> name is encoded to something like this
> >>
> >>
> SSESSae42ac488e03c2e2xxxxxda6ce2f5ee7=xxxxxxxxxxxx
> >> xxxxxxxxxxxxxxxxxx
> >>
> >> I've omitted some letters and numbers.  So my
> >> question is how do I just extract the Drupal
> >> Session cookie?  Or failing that just strip all
> >> cookies?  I have tried fastcgi_ignore header
> >> '__utma' 'Set-Cookie' but it doesn't seem to
> work.
> >
> >
> > Maybe extract the session cookie with something
> like this:
> >
> > map $http_cookie $session_cookie {
> > default "";
> > ~^.*(?<key>SESS.+\;).*$ $key;
> > }
> 
> Yes. We can remove the SESS string and keep just
> the unique part of the
> id.
> 
> map $http_cookie $session_id {
>     default '';
>     ~SESS(?<session_guid>[[:alnum:]]+)
> $session_guid;
> }
> 
> --- appa
> 

That regex will only get you the cookie name (md5 of domain) without the
cookie value.  You will want the equal sign in there so you get both
name and value:

~^.*SESS(?<key>[\w\=]+).*$ $key;

or just the value:

~^.*SESS.+\=(?<key>[\w]+).*$ $key;

Posted at Nginx Forum: http://forum.nginx.org/read.php?2,220510,220568#msg-220568



More information about the nginx mailing list