I am finally getting somewhere with configuring varnish to run in front of typo. I just need to add code to purge the varnish cache when pages are expired and add ESI support so page can be semi cached in varnish and composed on demand. The following is the code so fare. First adding PURGE to net/http
I also had to make changes to the PageCache class zap_pages method so that it forced varnish to purge the cache
app/models/page_cache.rb
123456789101112131415161718192021
defself.zap_pages(paths)logger.debug"PageCache - About to zap: #{paths.inspect}"srcs=paths.inject([]){|o,v|o+Dir.glob(public_path+"/#{v}")}varnish_purge(paths)returntrueifsrcs.empty?logger.debug"PageCache - About to delete: #{srcs.inspect}"trash=RAILS_ROOT+"/tmp/typodel.#{UUID.random_create}"FileUtils.makedirs(trash)FileUtils.mv(srcs,trash,:force=>true)FileUtils.rm_rf(trash)enddefself.varnish_purge(paths)paths.split.flatten.eachdo|path|uri="http://blog.vortorus.net/#{path}"Net::HTTP.purge(uri)logger.debug"Varnish purging #{uri}"endend
The is also the varnish vcl file changes to make purge work
subvcl_recv{# Host header check.if(!req.http.host~"blog.vortorus.net"){error404"Unknown virtual host";}if(req.request!="GET"&&req.request!="HEAD"){if(req.request=="PURGE"){if(client.ip~purge){purge_url(req.url);error200"Purged";}}pipe;}if(req.url~"\.(png|gif|jpg|swf|css|js)$"){removereq.http.cookie;removereq.http.authenticate;lookup;}# only need session for admin functionsif(req.url~"^/(admin|acounts)"){pipe;}else{# disable Etagsremovereq.http.If-None-Match;removereq.http.cookie;removereq.http.authenticate;lookup;}}subvcl_hit{deliver;}subvcl_fetch{if(req.url~"\.(png|gif|jpg|swf|css|js)$"){deliver;}if(req.url~"^/(admin|acounts)"){pass;}else{removeobj.http.Set-Cookie;removeobj.http.Etag;setobj.http.Cache-Control="no-cache";setobj.ttl=7d;deliver;}}
The source for the varnish rails plugin can be found hear