How do we configure symfony 1.4 to use it with Varnish?
I finally found on my own how to resolve my problem, so I'm going to share how I did it :
To start, you should know that symfony creates automatically a PHP session every time a page is called. So, what I did was to deactivate that default behaviour. To do so, I added on the factories.yml a storage factory (the default one : sfSessionStorage) with the auto_start parameter set to false:
storage: class: sfSessionStorage param: auto_start: false
Then, I created a filter to handle the http headers :
I firstly added it to the filters.yml file
http_header: class: myHttpHeaderFilter
And then I added a myHttpHeaderFilter.php class on the lib folder, where I handled all the headers I wanted. For example :
class myHttpHeaderFilter extends sfFilter{ public function execute($filterChain) { //execute the next filter in the chain $filterChain->execute(); //code after here runs after action // Filters don't have direct access to the request and user objects. // You will need to use the context object to get them $request = $this->getContext()->getRequest(); $path = $request->getPathInfo(); if (substr($path, 0, 5) == "/foo") // We cache only some of the /foo requests { if (strstr($path, "bar")) // We cache the request containing bar during half an hour hour $this->getContext()->getResponse()->addCacheControlHttpHeader('max-age=1800'); else // All the other requests are cached during 24 hours { $this->getContext()->getResponse()->addCacheControlHttpHeader('max-age=86400'); } } else // The other requests are not cached $this->getContext()->getResponse()->addCacheControlHttpHeader('no-cache, no-store'); }}
And that was it !
I also modified the vcl_recv on the server side, to ensure that all requests that not need to be cached are not (in theory, it is not mandatory to do so, because I handled it on symfony, it is just a "double-check").
sub vcl_recv { if (req.http.Authorization || req.http.Cookie) { /* Not cacheable by default */ return (pass); } if (req.request != "GET" && req.request != "HEAD") { /* We only deal with GET and HEAD by default */ return (pass); } if (req.url ~ "/user") /* Requests containing user data are never cached */ { return (pass); } return (lookup);}