Last active
February 28, 2024 12:56
-
-
Save rezan/1eadaef1745286a4e7262d83e1eff19c to your computer and use it in GitHub Desktop.
Self routing Varnish Cache cluster example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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