Help: cache or not by cookie

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

Help: cache or not by cookie

vedranf
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?

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


Reply | Threaded
Open this post in threaded view
|

Re: Help: cache or not by cookie

Igor Sysoev
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.


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

Reply | Threaded
Open this post in threaded view
|

Re: Help: cache or not by cookie

vedranf
In reply to this post by vedranf
Smart way, it should work. I will try.

Thank you, Igor!

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


Reply | Threaded
Open this post in threaded view
|

Re: Help: cache or not by cookie

Igor Sysoev
In reply to this post by Igor Sysoev
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/

Reply | Threaded
Open this post in threaded view
|

Re: Help: cache or not by cookie

vedranf
In reply to this post by vedranf
This infomation "proxy_pass_header Set-Cookie;" is helpful,  I have noticed some cookie related problem.

Thanks, Igor.

And would you take a loot at another problem:

http://forum.nginx.org/read.php?2,3196

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


Reply | Threaded
Open this post in threaded view
|

Re: Help: cache or not by cookie

vedranf
In reply to this post by vedranf
Hello,

Sorry for bumping this topic but I was trying to setup a similar configuration.
I want to show cached pages for a refresh of 6 min to guests (cookie user=guest) and to members (cookie user=usergroup)

Am I able to read the value from the cookie? How?
How do I add X-Accel-Expires: 0 to nginx?

Thanks

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


Reply | Threaded
Open this post in threaded view
|

Re: Help: cache or not by cookie

Igor Sysoev
On Wed, Jul 08, 2009 at 06:40:49PM -0400, nfn wrote:

> Hello,
>
> Sorry for bumping this topic but I was trying to setup a similar configuration.
> I want to show cached pages for a refresh of 6 min to guests (cookie user=guest) and to members (cookie user=usergroup)

proxy_cache_key    "http://cacheserver$request_uri $cookie_user";
proxy_cache_valid  6m;

> Am I able to read the value from the cookie? How?
> How do I add X-Accel-Expires: 0 to nginx?

You should add this header in backend.


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

Reply | Threaded
Open this post in threaded view
|

Re: Help: cache or not by cookie

vedranf
In reply to this post by vedranf
Thanks Igor, but how do I validate if the user is guest. I only want to show the cached version to guests.

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


Reply | Threaded
Open this post in threaded view
|

Re: Help: cache or not by cookie

Igor Sysoev
On Thu, Jul 09, 2009 at 06:05:26AM -0400, nfn wrote:

> Thanks Igor, but how do I validate if the user is guest. I only want to show the cached version to guests.

You need to use
   proxy_cache_key    "http://cacheserver$request_uri $cookie_user";
   proxy_cache_valid  6m;

Then in your backend you should test the "user" cookie. If it is not equal
to "guest", then you should add the "X-Accel-Redirect: 0" header in a
response. In this case nginx will not cache the response. Otherwise,
if cookie is equal to "guest" you do not need to set the header or may set
"X-Accel-Redirect: 360" to say nginx to cache the response to 6 min.


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

Reply | Threaded
Open this post in threaded view
|

Re: Help: cache or not by cookie

vedranf
In reply to this post by vedranf
If you use the proxy_cache_key directive as you demonstrated above (i.e. proxy_cache_key "http://cacheserver$request_uri$cookie_user";) then my understanding is:

If a user doesn't have that cookie set, the page will be cached, and all users who don't have the cookie set will see the same page.

If a user does have that cookie set, then the page will also be cached, but it will only be served to people who have identical values for that cookie (in my case this means users with that session key).

Is this correct?

The problem is that that isn't how it appears to be working at the moment. I've put the $cookie_sessionkey variable in my proxy_cache_key and I'm basically seeing the "logged in page" for both browsers with and without the cookie set :-(

I'm using nginx 0.7.61 (from Debian Sid)

Thanks,

Martyn

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


Reply | Threaded
Open this post in threaded view
|

Re: Help: cache or not by cookie

Igor Sysoev
On Thu, Sep 03, 2009 at 06:53:41PM -0400, ned0r wrote:

> If you use the proxy_cache_key directive as you demonstrated above (i.e. proxy_cache_key "http://cacheserver$request_uri$cookie_user";) then my understanding is:
>
> If a user doesn't have that cookie set, the page will be cached, and all users who don't have the cookie set will see the same page.
>
> If a user does have that cookie set, then the page will also be cached, but it will only be served to people who have identical values for that cookie (in my case this means users with that session key).
>
> Is this correct?

Yes.

> The problem is that that isn't how it appears to be working at the moment. I've put the $cookie_sessionkey variable in my proxy_cache_key and I'm basically seeing the "logged in page" for both browsers with and without the cookie set :-(
>
> I'm using nginx 0.7.61 (from Debian Sid)

Could you create denug log ?


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