Monday 17 December 2007 9:53:46 pm
Hi Norman, Thanks a lot for your quick reply. I agree with you that varnish does not cache documents when cookies are present by default, so we forced varnish to insert and lookup even when cookies are present as well. Besides, I think we found a way to cache dynamic documents as the followings.
Step 1. We forced varnish to "lookup" even when cookies are present just like what you did with minor differences in code base.
//just for testing purpose, we didn't specify multiple backends
sub vcl_recv {
//general setttings
if (req.request != "GET" && req.request != "HEAD") {
pipe;
}
if (req.http.Expect) {
pipe;
}
if (req.http.Authenticate) {
pass;
}
//this part ignores no-cache documents
if (req.http.Cache-Control ~ "no-cache") {
pass;
}
//other documents will be looked up from varnish even when cookies are present
lookup;
}
Step 2. Insert documents into cache even when cookies are present
sub vcl_fetch {
if (!obj.valid) {
error;
}
if (!obj.cacheable) {
pass;
}
//set-cookie response should be passed
//for example, eZ Publish session handler may
//set session cookie the first time when you visit the site
if(obj.http.Set-Cookie){
pass;
}
//no-cache documents should be ignored and passed
if(obj.http.Pragma ~ "no-cache" ||
obj.http.Cache-Control ~ "no-cache" ||
obj.http.Cache-Control ~ "private"){
pass;
}
//other documents should be inserted into
//varnish cache, even when cookies are present
insert;
}
Step 3. The most important step: change the vcl_hash subroutine.
sub vcl_hash {
//hash the object with url+host
set req.hash += req.url;
set req.hash += req.http.host;
/*
uncomment the following code snippet if you also want to hash the
object with cookie.
If you don't uncomment this line, different users
may share same object in cache.
Otherwise, varnish will insert/fetch a cache object for
each user
*/
//set req.hash += req.http.cookie;
hash;
}
Step 4. configure "customheader" in site.ini.append.php. Just for testing purpose, we made the "/" cacheable like this:
[HTTPHeaderSettings]
CustomHeader=enabled
Cache-Control[]
Cache-Control[/]=
Pragma[]
Pragma[/]=
Expires[]
Expires[/]=1200
Step 5. Optional - if you need login function, you can implement a login panel with Ajax. So each time the document is loaded, an ajax function will be invoked to either display login panel or welcome message. You can do this with xajax extension. We have implemented the solution above. And in order to test the performance improvement, we disabled all template cache and view cache on eZ Publish. Here is the test cases: Test case 1: 1. telnet to varnish management port 2. url.purge .* 3. access http://<varnish server>:<varnish port>/ from machine A 4. It takes around 20 seconds to load the page 5. re-load current page and this time it takes less than 1 second 6. try to access http://<varnish server>:<varnish port>/ from machine B 7. access http://<varnish server>:<varnish port>/ from machine B 8. It takes less than 1 second to load the page. ( which means machine B is using the same cache with machine A) Test Case 2: 1. Change the vcl_hash() subroutine and set hash to "url+host+cookie" 2. Kill and restart varnish 3. Repeat the steps in Test Case 1 This time, machine B can't re-use the cache with machine A. But on both machine, after re-loading current page, the document will be cached by varnish. Basically, that's all we have investigated so far. Could you please review it and get back to me with your comments? Kind regards, Michael
Michael Lee | Managing Director | ZerusTech Ltd | www.zerustech.com
Skype: zerustech
|