Help: cache or not by cookie

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

On Sat, Jun 20, 2009 at 06:54:57AM -0400, tonyy wrote:

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.

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

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

set  $cache  0;

}

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.

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

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”:wink: 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 :frowning:

I’m using nginx 0.7.61 (from Debian Sid)

Could you create denug log ?

If you use the proxy_cache_key directive as you demonstrated above (i.e.
proxy_cache_key “http://cacheserver$request_uri$cookie_user”:wink: 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 :frowning:

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs