Welcome! Log In Create A New Profile

Advanced

Proxy_Cache

Posted by Agregs 
Proxy_Cache
February 03, 2011 04:30PM
I have been experiancing the following after implimenting Caching:

Cache Configuration under location /

proxy_set_header X-Real-IP $remote_addr;
proxy_ignore_headers X-Accel-Expires Expires Cache-Control;
proxy_cache large;
proxy_bypass_cache $cookie_sdata;
proxy_no_cache $cookie_sdata;
proxy_cache_key $scheme$host$request_uri;
proxy_cache_valid any 10m;

My hope was that if a user came through without a sdata cookie they would hit the cache, and if they did they wouldn't. THe problem is if a user with an sdata cookie hits the page first it is stored in cache for the user without the sdata cookie to retrieve it.

In reading the proxy_no_cache on the wiki this seems to be by design, so I am a little confused on how I can turn caching on and off with a cookie and not have the cache store the cached response.

In the interm I have added the cookie to the cache key, but there is another problem there as the cookie is not wiped / or altered upon logout, so the user always remains logged in.
Re: Proxy_Cache
February 07, 2011 04:03PM
So I came up with a work around, this is specific as I had to bypass a cookie being set with every request.

I used a varible $miss_it this ensured that i would or would not be passing to the backend that would ignore the set-cookie header when a cookie existed or did not exist. I have the cookie only being set by a specific location which does not have the proxy_cache for that specific reason. I ended up doing it this way as I could not set the proxy_ignore_headers as a varible (which makes sense).

set $do_not_cache 0;
set $cache_key $scheme://$host$uri$is_args$args;
set $miss_it 1;

if ($http_cookie ~* "sdata" ) {
set $do_not_cache 1;
set $cache_key $scheme://$host$uri$is_args$args$cookie_sdata;
set $miss_it virtual.apache;
}

location ~* ^/blog {
include /etc/nginx/proxy.conf;
include /etc/nginx/gzip.conf;
include /etc/nginx/conf/cache-nocookie.conf;
if ($miss_it = 1 ) {
proxy_pass http://$miss_it;
error_page 502 = /apachecachecookie$uri;
break;
}
proxy_pass http://virtual.apache;
break;
}


location ^~ /apachecachecookie {
internal;
include /etc/nginx/proxy.conf;
include /etc/nginx/gzip.conf;
include /etc/nginx/conf/cache-nocookie.conf;
proxy_pass http://virtual.apache;
proxy_hide_header Set-Cookie;
proxy_ignore_headers X-Accel-Expires Cache-Control Expires Set-Cookie;
break;
}



cache-nocookie.conf:

if ($do_not_cache = 0){

set $cache_proxy_bypass 0;
set $cache_proxy_no_cache 0;
set $cache_cache_friendly_head hit;

}


if ($do_not_cache = 1){

set $cache_proxy_bypass 1;
set $cache_proxy_no_cache 1;
set $cache_cache_friendly_head miss;

}

if ($cache_cache_apache = miss) {

set $cache_cache_friendly_head miss;

}


proxy_ignore_headers X-Accel-Expires Cache-Control Expires;

proxy_cache_bypass $cache_proxy_bypass; #if 1 then bypass cache for response
proxy_no_cache $cache_proxy_no_cache; # if 1 then don't cache response

proxy_cache large;
add_header x-cachencache $cache_cache_friendly_head;
add_header x-cachecachekey $cachekey; # for debug see what cache key is

proxy_cache_key $cachekey;

add_header x-cacheproxy proxyname;
proxy_cache_valid 200 301 302 404 10m;
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 260
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready