我有以下形式的 URL:
http://some-site.com/api/v2/portal-name/some/webservice/call
我要获取的数据需要
http://portal-name.com/webservices/v2/some/webservice/call
(是的,我可以重写应用程序,以便它使用其他 URL,但我们目前正在测试 Varnish ,因此目前它不能被侵入。)
但是我无法在 Varnish VCL 中正确获取 URL。 api
部分被替换为空字符串,不用担心,但现在是门户名称。
我尝试过的事情:
if (req.url ~ ".*/(.*)/") {
set req.http.portalhostname = re.group.0;
set req.http.portalhostname = $1;
}
来自 https://docs.fastly.com/guides/vcl/vcl-regular-expression-cheat-sheet和 Extracting capturing group contents in Varnish regex
是的,std
是导入的。
但这给了我一个
Syntax error at
('/etc/varnish/default.vcl' Line 36 Pos 35)
set req.http.portalhostname = $1;
或一个
Symbol not found: 're.group.0' (expected type STRING_LIST):
那么:我该怎么做呢?当我提取 portalhostname
时,我应该能够简单地执行 regsub
以用空字符串替换该值,然后在前面加上“webservices”,我的 URL 就完成了。
我使用的 Varnish 版本:varnish-4.1.8 revision d266ac5c6
最佳答案
遗憾的是,re.group 似乎已在某些版本中被删除。类似的功能似乎可以通过几个 vmod 之一访问。参见 https://varnish-cache.org/vmods/
关于Varnish VCL 中的正则表达式捕获组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47159659/