Redirect to another backend based on response status
In this example, if a certain request comes in that causes a backend to return both a status code of 409
and if that same backend sends a header X-Fastly-Instructions
with the contents "Use Other Backend"
, we restart the request, and point to a new backend.
sub vcl_recv {
#FASTLY recv
# This header disables intra-POP clustering, allowing a request to process
# through the same Varnish machine, rather than a cluster. We don't reccomend
# setting this unless Fastly support advises
set req.http.Fastly-No-Shield = "1";
# If the header `Redirect-Header` is present and equal to 1, redirect to
# a new backend, and set original-host equal to the previous host
if (req.http.Redirect-Header == "1") {
set req.http.original-host = req.http.host;
set req.backend = F_my_new_backend_com;
return(pass);
}
return(lookup);
}
sub vcl_fetch {
# If the backend returns a status of 409 and the header
# X-Fastly-Instructions:"Use Other Backend" is returned, set Redirect-Header:1
# in the request and restart. This will send the request back to vcl_recv and
# trigger the backend redirection logic.
if (beresp.status == 409 && beresp.http.X-Fastly-Instructions == "Use Other Backend") {
set req.http.Redirect-Header = "1";
restart;
}
}
sub vcl_hash {
# This adds Original-URL and Original-Host to the cache key. We absolutely
# recommend _not_ changing the cache key unless so advised by Fastly support.
set req.hash += req.http.original-url;
set req.hash += req.http.original-host;
#FASTLY hash
}
Please sign in to leave a comment.
Comments
2 comments