Skip to content

Instantly share code, notes, and snippets.

@rezan
Last active February 28, 2024 12:56
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save rezan/1eadaef1745286a4e7262d83e1eff19c to your computer and use it in GitHub Desktop.
Save rezan/1eadaef1745286a4e7262d83e1eff19c to your computer and use it in GitHub Desktop.
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