Skip to content

Instantly share code, notes, and snippets.

@smithcommajoseph
Created October 27, 2011 21:40
Show Gist options
  • Save smithcommajoseph/1320962 to your computer and use it in GitHub Desktop.
Save smithcommajoseph/1320962 to your computer and use it in GitHub Desktop.
Sample Localhost VCL config for Varnish 3 and Rails 3.1
#-e This is a basic VCL configuration file for varnish. See the vcl(7)
#man page for details on VCL syntax and semantics.
#
#Default backend definition. Set this to point to your content
#server.
#
backend default {
.host = "127.0.0.1";
.port = "3000";
}
sub vcl_recv {
#in vcl_revc you can "pass" , "pipe", or "lookup"
#never cache non-GET methods
if (req.request != "GET") {
return(pipe); #pass will accomplish same, but apparently more varnish handling
}
# #allow static assets automatically
# if (req.url ~ "\.(pdf|png|gif|jpg|swf|css|js)(/$|/\?|\?|$)" ) {
# remove req.http.cookie;
# remove req.http.authenticate;
# remove req.http.Etag;
# remove req.http.If-None-Match;
# lookup; #go to backend and see if there is an object
# }
#
#dynamic ruleset for frontend caching, attempt to pull from cache on ALL GET requests.
if (req.request == "GET") {
# disable Etags && incoming cookies
remove req.http.cookie;
remove req.http.authenticate;
remove req.http.Etag;
remove req.http.If-None-Match;
return(lookup); #go to backend and see if there is an object
# i give up, no caching as default strategy
}
return(pass);
}
sub vcl_fetch {
# fetch happens when we pass or try lookup and miss or otherwise go to backend.
# we can make more assertions on the obj and possibly cache for future use.
# deliver == cache
# hit_for_pass == no caching
# very important, we don't want to cache 500s
# we are not letting varnish have a grace period to handle and error or slow backend.
if (beresp.status >= 300) {
return(hit_for_pass);
}
#
#respect the backend from Rails private, no caching here
if ( beresp.http.Cache-Control ~ "private") {
return(hit_for_pass);
}
# #to be pulled from cache, we need the public set
if (beresp.http.Cache-Control ~ "public") {
unset beresp.http.Set-Cookie;
#unset the cache control, else the browser will keep for that long too.
#we want to control requests.
unset beresp.http.Etag;
unset beresp.http.Cache-Control;
set beresp.http.Cache-Control = "no-cache"; #tell client to request a new one everytime
set beresp.do_esi = true; #we will attempt to ESI process as well
return(deliver);
}
#else we will try NOT cache by default , be safe
return(hit_for_pass);
}
# this executes on every response from varnish
sub vcl_deliver {
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT";
} else {
set resp.http.X-Cache = "MISS";
}
}
# #This is displayed if there is an error.
# still have to nail the newer syntax for this...
# sub vcl_error {
# set obj.http.Content-Type = "text/html; charset=utf-8";
# synthetic {"
# <?xml version="1.0" encoding="utf-8"?>
# <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
# "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
# <html>
# <head>
# <title>"} obj.status " " obj.response {"</title>
# </head>
# <body>
# <h1>Error "} obj.status " " obj.response {"</h1>
# <p>"} obj.response {"</p>
# <br>Request URL: "} req.url {"
# <br>Request Host: "} req.http.host {"
# <h3>Guru Meditation:</h3>
# <p>XID(): "} req.xid {"</p>
#
# <address><a href="http://urlhere/">Some error msg</a></address>
# </body>
# </html>
# "};
# deliver;
# }
#!/bin/bash
#Interactive Varnish startup script. Does not run in daemon mode, mostly designed for OSX development.
#Make sure your IPv6 /etc/hosts entries are commented out or you'll get this error:
# Only one address is allowed.
# Please specify which exact address you want to use, we found these:
# ::1
# fe80::1%lo0
# 127.0.0.1
#One line example, default VCL routines built into Varnish.
#/usr/local/sbin/varnishd -a blackbook.local:3000 -b blackbook.local:8080 -T blackbook.local:6082
#start up Varnish, listening on localhost:6081, with default backend to Rails on 3000, management on port 6082, storage set for 50m in /tmp,
BASE=`pwd`
varnishd -a localhost:6081 -T localhost:6082 -F -s file,/tmp/varnish.storage,50M -f ${BASE}/config/app.vcl
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment