Can I cache a page conditionally based on cookie value?

patng323 nginx-forum at
Wed May 15 01:23:08 UTC 2013


Using NginX as a reverse proxy, I want to cache the response ONLY if the
user has NOT logged in, which can be checked by testing the existence of a

First I tried to use "if", but NginX complained that I cannot put a
proxy_cache_valid inside an "if":

location / {
if ($cookie_login = '') {
proxy_cache my-cache;
proxy_cache_valid 200 301 302 60m;

Then I tried to set the cache-duration to 0s by using a variable, but then
NginX complained the time value is incorrect.
set $cacheDuration 60m;
if ($cookie_login != '') {
set $cacheDuration 0s;

proxy_cache my-cache;
proxy_cache_valid 200 301 302 $cacheDuration

So is there any way to cache conditionally based on the existence of a


- Patrick

Posted at Nginx Forum:,239207,239207#msg-239207

More information about the nginx mailing list