Forum: NGINX Re: proxy_cache_bypass and proxy_no_cache

Posted by "António P. P. Almeida" <appa@perusio.net> (Guest)
on 2012-12-10 09:30
(Received via mailing list)
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 <nginx-forum@nginx.us> a écrit :
Posted by amodpandey (Guest)
on 2012-12-10 10:02
(Received via mailing list)
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: 
http://forum.nginx.org/read.php?2,233778,233783#msg-233783
Posted by Antonio P.P. Almeida (Guest)
on 2012-12-10 10:28
(Received via mailing list)
> But the first request which does not have the cookie is getting cached.
Try:

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

--appa
Posted by amodpandey (Guest)
on 2012-12-10 10:32
(Received via mailing list)
I was trying this :)

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

Let me try your suggestion.

Posted at Nginx Forum: 
http://forum.nginx.org/read.php?2,233778,233785#msg-233785
Posted by amodpandey (Guest)
on 2012-12-10 11:16
(Received via mailing list)
Thank you :) 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: 
http://forum.nginx.org/read.php?2,233778,233786#msg-233786
Posted by amodpandey (Guest)
on 2012-12-10 11:22
(Received via mailing list)
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: 
http://forum.nginx.org/read.php?2,233778,233788#msg-233788
Posted by Igor Sysoev (Guest)
on 2012-12-10 12:23
(Received via mailing list)
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 Sysoev
Posted by Igor Sysoev (Guest)
on 2012-12-10 12:24
(Received via mailing list)
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 Sysoev
http://nginx.com/support.html
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.