Hey everyone, I’m trying to get Varnish 1.0.3-2 set up so that it falls back to a secondary backend when the primary one returns a 404. In an older version (around 0.5), we used the ‘restart’ action to achieve this, but it doesn’t seem to be supported in the new version.
I’ve reviewed the documentation and various online examples, but many of them are outdated. The current man 7 vcl page is the one that seems to reflect what we’re dealing with. Has anyone managed to configure this in the newer version of Varnish? Or is there another approach to obtain similar behavior?
If Varnish isn’t capable of this, I would greatly appreciate alternatives. Below is a simplified snippet of our previous configuration:
sub vcl_receive {
if (request_count == 0) {
use_backend('primary');
} else if (request_count == 1) {
use_backend('secondary');
}
}
sub vcl_fetch {
if (response_code != 200 && response_code != 302) {
try_again();
}
}
Any suggestions or insights would be really helpful. Thanks!
hey there! have u tried health probes on your primary? they can auto swtich varnish to secondary if the probe fails. for example, set up a probe in your backend config. do u think this approach could simplify your setup?
hey mate, i’ve been messing with varnish 1.0.3-2 too. have u tried using the ‘synth’ function? it’s kinda like ‘error’ but more flexible. u could do somethin like:
sub vcl_backend_response {
if (beresp.status == 404) {
return(synth(800, ‘Fallback’));
}
}
sub vcl_synth {
if (resp.status == 800) {
set req.backend_hint = secondary;
return(restart);
}
}
I’ve encountered a similar challenge with Varnish 1.0.3-2. The solution I found was to use the ‘error’ function instead of ‘restart’. Here’s how I approached it:
In vcl_fetch, you can check for the 404 status and then call the error function with a custom status code. Then, in vcl_error, you can handle this custom status by changing the backend and restarting the request.
Something like this worked for me:
sub vcl_fetch {
if (beresp.status == 404) {
error 800 'Fallback';
}
}
sub vcl_error {
if (obj.status == 800) {
set req.backend = secondary_backend;
return(restart);
}
}
This method effectively achieves the fallback behavior you’re looking for. Remember to adjust the backend names and error code as needed for your specific setup.