Old thread: Cache for non-cookie users and fresh for cookie users

Maxim Dounin mdounin at mdounin.ru
Thu Feb 9 08:49:50 UTC 2012


On Thu, Feb 09, 2012 at 12:34:33PM +0530, Quintin Par wrote:

> Picking up an old thread for caching
> http://nginx.2469901.n2.nabble.com/Help-cache-or-not-by-cookie-td3124462.html
> Igor talks about caching by
> “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.”
> But from my understanding of “*X-Accel-Expires” *it expires the cache in
> the cache repository as given below
> “Sets when to expire the file in the internal Nginx cache, if one is used.”
> Does this not mean that when I set cookie and pass “X-Accel-Expires: 0” it
> expires the cache for the non logged in user too, for that cache key? A new
> cache entry will then have to be created, right?

No.  X-Accel-Expires will prevent the particular response from 
being cached, but won't delete existing cache entry.

> Should I go with “Cache-Control: max-age=0” approach?

The only difference between "X-Accel-Expires: 0" and 
"Cache-Contro: max-age=0" is that the former won't be passed to 

As for the use-case in general (i.e. only use cache for users 
without cookie), in recent versions it is enough to do

    proxy_cache_bypass $cookie_name;
    proxy_no_cache $cookie_name;

I.e.: don't respond from cache to users with cookie 
(proxy_cache_bypass), don't store to cache responses for users 
with cookie (proxy_no_cache).

Moreover, responses with Set-Cookie won't be cached by default, 
too.  So basically just placing the above into config is enough, 
no further changes to a backend code required.

Maxim Dounin

More information about the nginx mailing list