Caching for a location based on cookie

Vinay Y S vinay.ys at
Fri Jan 15 11:06:56 MSK 2010

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*

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the nginx mailing list