Skip to content

Instantly share code, notes, and snippets.

@gquintard
Created March 10, 2017 14:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gquintard/ca3f60cc2790558b911f557ba4df8cf0 to your computer and use it in GitHub Desktop.
Save gquintard/ca3f60cc2790558b911f557ba4df8cf0 to your computer and use it in GitHub Desktop.
Varnish sticky session (new)
varnishtest "Test cookie vmod"
server s1 {
rxreq
txresp -hdr "server: s1" -hdr "set-cookie: id=123;"
rxreq
txresp -hdr "server: s1"
rxreq
txresp -hdr "server: s1"
} -start
server s2 {
rxreq
txresp -hdr "server: s2" -hdr "set-cookie: id=456;"
rxreq
txresp -hdr "server: s2"
rxreq
txresp -hdr "server: s2"
} -start
varnish v1 -vcl+backend {
import cookie from "${modules_builddir}/.libs/libvmod_cookie.so";
import directors from "${topbuild}/lib/libvmod_directors/.libs/libvmod_directors.so";
sub vcl_recv {
cookie.parse(req.http.cookie);
if (!cookie.get("id")) {
if (req.url == "/") {
cookie.set("id", "s1");
} else {
cookie.set("id", "s2");
}
}
set req.http.server = cookie.get("id");
if (req.http.server == "s1") {
set req.backend_hint = s1;
} else if (req.http.server == "s2") {
set req.backend_hint = s2;
}
return (pass);
}
sub vcl_deliver {
cookie.parse(resp.http.set-cookie);
cookie.set("id", req.http.server);
set resp.http.set-cookie = cookie.get_string();
}
} -start
client c1 {
txreq -url "/"
rxresp
expect resp.http.set-cookie == "id=s1;"
expect resp.http.server == "s1"
txreq -url "/index.html"
rxresp
expect resp.http.set-cookie == "id=s2;"
expect resp.http.server == "s2"
txreq -hdr "cookie: id=s2;"
rxresp
expect resp.http.server == "s2"
txreq -hdr "cookie: id=s1;"
rxresp
expect resp.http.server == "s1"
txreq -hdr "cookie: id=s1;"
rxresp
expect resp.http.server == "s1"
txreq -hdr "cookie: id=s2;"
rxresp
expect resp.http.server == "s2"
} -run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment