I am trying to implement a VCL snippet (for Fastly Varnish implementation), that would replace URL before proxying the request to a service.
Eg.:
//domain.com/services/user/hello-world
-> //userservice/hello-world
To do this, I have written this:
if (req.url.path ~ "^/services/") {
set req.url = regsub(req.url, "/services/(.*?)/", "/");
}
This should be applied just before the backend fetch so that it does not have any impact on caching or other conditions. From the documentation this should be put inside vcl_pass
. Is that correct? vcl_fetch
seems to be applied too late for this.
This is edited within Fastly settings as:
This generates following vcl_pass
code: https://gist.github.com/knyttl/ae1a3afbc219e311ace3d7d305f9142d
However, it does not work as the target backend receives the full, non-modified url. What am I doing wrong? Is vcl_pass
correct stage to do this?
If you modify the path in
recv
, this will be taken into account in thehash
subroutine meaning that caching will not be affected.See your example demonstrated in this Fiddle: https://fiddle.fastlydemo.net/fiddle/53bf8ab6
vcl_pass
will only run if you returnpass
from the recv, miss, or hit subroutines as documented here: https://developer.fastly.com/reference/vcl/subroutines/pass/