Skip to content

Commit

Permalink
Removing the VCL from the README now the generator works.
Browse files Browse the repository at this point in the history
  • Loading branch information
russ committed Nov 3, 2010
1 parent 90f89cd commit ba26fc5
Showing 1 changed file with 3 additions and 117 deletions.
120 changes: 3 additions & 117 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -60,129 +60,15 @@ Clearing the cache:

== Gotchas

The default TTL for most actions is set to 0, since for most cases you'll probably want to be fairly explicit about what pages do get cached by varnish. The default cache header is typically
The default TTL for most actions is set to 0, since for most cases you'll probably want to be fairly explicit about what pages do get cached by varnish. The default cache header is typically:

Cache-Control: max-age=0, no-cache, private

This is good for normal controller actions, since you won't want to cache them. If TTL for an action is set to 0, it won't mess with the default header.
This is good for normal controller actions, since you won't want to cache them. If TTL for an action is set to 0, it won't mess with the default header.

The key gotcha here is that cached pages strip cookies, so if your application relies on sessions and uses authenticity tokens, the user will need a session cookie set before form actions will work. Setting default TTL to 0 here will make sure these session cookies won't break.

As a result, all you have to do to set a cacheable action is the before filter above.

== VCL config

# -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 = "8282";
}

# Handling of requests that are received from clients.
# First decide whether or not to lookup data in the cache.
sub vcl_recv {
# Pipe requests that are non-RFC2616 or CONNECT which is weird.
if (req.request != "GET" &&
req.request != "HEAD" &&
req.request != "PUT" &&
req.request != "POST" &&
req.request != "TRACE" &&
req.request != "OPTIONS" &&
req.request != "DELETE") {
return(pipe);
}

# Pass requests that are not GET or HEAD
if (req.request != "GET" && req.request != "HEAD") {
return(pass);
}

# Pass requests that we know we aren't caching
if (req.url ~ "^/admin") {
return(pass);
}

#
# Everything below here should be cached
#

# Handle compression correctly. Varnish treats headers literally, not
# semantically. So it is very well possible that there are cache misses
# because the headers sent by different browsers aren't the same.
# @see: http://varnish.projects.linpro.no/wiki/FAQ/Compression
if (req.http.Accept-Encoding) {
if (req.http.Accept-Encoding ~ "gzip") {
# if the browser supports it, we'll use gzip
set req.http.Accept-Encoding = "gzip";
} elsif (req.http.Accept-Encoding ~ "deflate") {
# next, try deflate if it is supported
set req.http.Accept-Encoding = "deflate";
} else {
# unknown algorithm. Probably junk, remove it
remove req.http.Accept-Encoding;
}
}

# Clear cookie and authorization headers, set grace time, lookup in the cache
# unset req.http.Cookie;
# unset req.http.Authorization;
set req.grace = 1s;
return(lookup);
}

# Called when entering pipe mode
sub vcl_pipe {
# If we don't set the Connection: close header, any following
# requests from the client will also be piped through and
# left untouched by varnish. We don't want that.
set req.http.connection = "close";
return(pipe);
}


# Called when the requested object has been retrieved from the
# backend, or the request to the backend has failed
sub vcl_fetch {
# Do not cache the object if the backend application does not want us to.
if (obj.http.Cache-Control ~ "(no-cache|no-store|private|must-revalidate)") {
pass;
}

# Do not cache the object if the status is not in the 200s
if (obj.status >= 300) {
# Remove the Set-Cookie header
remove obj.http.Set-Cookie;
return(pass);
}

# Everything below here should be cached

# Remove the Set-Cookie header
remove obj.http.Set-Cookie;

# Set the grace time
set obj.grace = 1s;

# Deliver the object
deliver;
}

sub vcl_deliver {
# Force browsers and intermediary caches to always check back with us
# set resp.http.Cache-Control = "private, max-age=0, must-revalidate";
# set resp.http.Pragma = "no-cache";

# Add a header to indicate a cache HIT/MISS
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT";
} else {
set resp.http.X-Cache = "MISS";
}
}

As a result, all you have to do to set a cacheable action is the before filter above.

== Note on Patches/Pull Requests

Expand Down

0 comments on commit ba26fc5

Please sign in to comment.