Re: proxy_cache_bypass and proxy_no_cache

You can use at the http level:

map $cookie_no_cache $dont_cache {
0 1;
1 0;
}

Then use the $dont_cache variable instead.

–appa

amodpandey [email protected] a écrit :

But the first request which does not have the cookie is getting cached.
Try:

map $cookie_cache $dont_cache {
default 1;
~.+$ 0;
}

–appa

I did this ( my cookie name is cahe )

map $cookie_cache $dont_cache {
    0 1;
    1 0;
}

proxy_cache_bypass $dont_cache;
proxy_no_cache $dont_cache;

But the first request which does not have the cookie is getting cached.

Posted at Nginx Forum:

I was trying this :slight_smile:

map $cookie_route $dont_cache {
    default 1;
    ~*[A-Za-z0-9]+ 0;
}

Let me try your suggestion.

Posted at Nginx Forum:

One interesting observation

I put this code

    location /test {
        proxy_pass http://a/;
        ....
        set $dont_cache 1;

        if ( $cookie_route ) {
                 set $dont_cache 0;
        }
        proxy_cache_bypass    $dont_cache;
        proxy_no_cache        $dont_cache;

    }

and it started to dis-honour the trailing slash http://a/ With the end /
it
does not look for /test but only due to the if block it started to look
for
it.

Posted at Nginx Forum:

On Dec 10, 2012, at 14:15 , amodpandey wrote:

So in this case even proxy_no_store $http_set_cookie does not work. So the
Why we need to have proxy_cache_bypass and proxy_no_cache else
nginx: [warn] “proxy_no_cache” functionality has been changed in 0.8.46, now
it should be used together with “proxy_cache_bypass”

By default nginx does not cache response with Set-Cookies header. So to
force to cache such response you should set:
proxy_ignore_headers Set-Cookie;

As to your problem, it’s better to resolve it on upstream side by
setting
X-Accel-Expires: 0
for the first response without Cookie, and
X-Accel-Expires: a_number_of_seconds_to_cache
for response with cookies.

Also you should $http_cookie_cache variable to
proxy_cache_key


Igor S.

On Dec 10, 2012, at 14:22 , amodpandey wrote:

                set $dont_cache 0;
       }
       proxy_cache_bypass    $dont_cache;
       proxy_no_cache        $dont_cache;

   }

and it started to dis-honour the trailing slash http://a/ With the end / it
does not look for /test but only due to the if block it started to look for
it.

Do not use “if”, use “map” instead.


Igor S.

Thank you :slight_smile: This one worked.

I would like to take this opportunity to share the exact problem I had.

I am using sticky module for some app version targetting. Plus I have
enabled proxy cache. The sticky module works in a way that it first let
the
upstream handle the weights and according to which server catered the
response it set cookie with server info hash. So the response that is
cached
is without cookie.

So in this case even proxy_no_store $http_set_cookie does not work. So
the
only way I could think was to change the caching behaviour to cache only
requests with my cookie.

There is only proxy_no_store ( proxy_store does something else ) so I
need
to negate the value, if set. Here the map helped me to achieve the same.

But I wish there could have been a straight forward way.

I had this followup question

Why we need to have proxy_cache_bypass and proxy_no_cache else
nginx: [warn] “proxy_no_cache” functionality has been changed in 0.8.46,
now
it should be used together with “proxy_cache_bypass”

Posted at Nginx Forum: