Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Self routing Varnish Cache cluster example
# Self routing cluster example
vcl 4.0;
import directors;
backend node1 {
.host = "node1.example.com";
.port = "80";
}
backend node2 {
.host = "node2.example.com";
.port = "80";
}
backend node3 {
.host = "node3.example.com";
.port = "80";
}
backend content {
.host = "content-origin.example.com";
.port = "80";
}
sub vcl_init
{
# We use a simple hash director to shard our content
new cluster = directors.hash();
cluster.add_backend(node1, 1);
cluster.add_backend(node2, 1);
cluster.add_backend(node3, 1);
}
sub vcl_recv
{
# Figure out where the content is
set req.backend_hint = cluster.backend(req.url);
set req.http.X-shard = req.backend_hint;
# Do we have the content? If yes, reroute to the backend
# If not, pass the request to the appropriate node
if (req.http.X-shard == server.identity) {
set req.backend_hint = content;
} else {
return(pass);
}
# Your VCL logic starts here
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.