Caching for a location based on cookie

Hi,
For a given location, I want the requests to be cached only if a
particular
cookie is not present. How do I achieve that with nginx?
Currently I’m trying following config

location /album {
    if ($cookie_logintoken ~* "") {
        set $cache_key $scheme$proxy_host$uri$is_args$args;
    }

    proxy_pass_header  Set-Cookie;
    proxy_cache_key $cache_key;
    proxy_cache mem_pool;
    proxy_cache_valid  200 5m;
    proxy_pass http://$backend$uri$is_args$args;

}

Here, if the logintoken cookie is not present in the request, the
cache_key
variable is set to a valid key string. If the cookie is present,
cache_key
it will be undefined. So I expect the proxy_cache_key directive to not
cache
that request. But it seems it still caches the request with empty key.
Is
this the expected behavior? Is there any solution this problem?

Also, with “proxy_cache_valid 200 5m;” I expect only 200 responses to be
cached. But I’m seeing 30x responses also getting cached.

nginx version 0.7.64

Thanks,

On Fri, Jan 15, 2010 at 4:06 PM, Vinay Y S [email protected] wrote:

Hi,
For a given location, I want the requests to be cached only if a particular cookie is not present. How do I achieve that with nginx?
Currently I’m trying following config
location /album {
if ($cookie_logintoken ~* “”) {
set $cache_key $scheme$proxy_host$uri$is_args$args;
}

Please see this thread for a simple solution:

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

Hope this helps.

Cheers,
-agentzh

On Fri, Jan 15, 2010 at 2:37 PM, agentzh [email protected] wrote:

Please see this thread for a simple solution:

Module vars set inside if's are not set

Hope this helps.

Could you please elaborate on it? There was no issues with $cache_key
value
being set in if block and being visible to proxy_cache_key directive.
The
issue is when the if condition is not satisfied and hence the $cache_key
is
not set, I was expecting the caching to get disabled altogether as
proxy_cache_key was now being passed a uninitialized variable as
parameter.

Thanks,
Vinay