Help: cache or not by cookie

Igor Sysoev is at rambler-co.ru
Mon Jun 22 15:32:24 MSD 2009


On Sat, Jun 20, 2009 at 05:24:15PM +0400, Igor Sysoev wrote:

> On Sat, Jun 20, 2009 at 06:54:57AM -0400, tonyy wrote:
> 
> > The requirement is:
> > 
> > When a cookie exist, not return cached file and not cache the response, if the cookie not exist, return cached file and . cache the response.
> > 
> > In the past, we use a standalone cacheserver, the configuration looks like below:
> > 
> > location / {
> >   if ($http_cookie ~* xxx ) {
> >     set  $cache  0;
> >   }
> > 
> >   if ($cache = 1) {
> >       proxy_pass  http://cacheserver;
> >       break;
> >   }
> > }
> > 
> > location ~* \.php {
> >   fastcgi_pass   127.0.0.1:8080;
> > }
> > 
> > So, cookie-non-exist visit will be directed to cacheserver, and cookie-exist visit will be directed to fastcgi_pass.
> > 
> > So if we want to remove the cacheserver, and use nginx's cache feature, we need to configure like this:
> > 
> > location ~* \.php {
> >   fastcgi_pass   127.0.0.1:8080;
> >   fastcgi_cache   php;
> >   fastcgi_cache_key $request_uri;
> > }
> > 
> > Then how can we set cookie-non-exist visits to use cache and  cookie-exist visit not to use cache?
> 
> No, currently the single way is:
> 
> 1) add the cookie in proxy_cache_key
>    proxy_cache_key  "http://cacheserver$request_uri $cookie_name";
> 
> 2) add "X-Accel-Expires: 0" in response with the cookie.
> 
> Then requests with the cookie will be sent to a backend since they will
> never be cached, and requests without the cookie will be cached.

Also, you need to add
    proxy_pass_header  Set-Cookie;
because nginx does not send "Set-Cookie" to client if response can be
cacheable.


-- 
Igor Sysoev
http://sysoev.ru/en/





More information about the nginx mailing list