如何编写清漆脚本来做一些非常特别的事情
Varnish 脚本对于 vcl 来说似乎相当强大,但我还不知道如何让它做我需要的事情。我从相同的代码库运行各个站点,并且我希望大多数目录有一个统一的清漆缓存,因此
x.mysite.org/theme/something.gif 和 y.mysite.org/theme/something.gif 不应存储两个副本清漆缓存中的相同 gif
但是
x.mysite.org/file.php/1 和 y.mysite.org/file.php/1 应该根据 url 有单独的缓存。
另外,mysite.org 是一个拥有自己的缓存的完全不同的网站。
我当前的方向如下,
sub vcl_fetch {
if (req.url ~ ".*\.org/file\.php") {
# do normal site specific caching
} elseif (req.url ~ "^+?\.mysite.org") {
# cache all found material in a base directory so everyone knows where to look
set req.url = regsub(req.url, "(.*\.org)(.*)", "base.mysite.org\2");
} else {
# do normal site specific caching for base site
}
}
sub vcl_recv {
# do I need to do something here to look in base.mysite.org
}
如果有必要,我可以使 base.mysite.org 成为一个真正的 apache 服务站点,这样如果没有缓存,请求就会失败。
我在写路径上吗,有什么帮助吗?
Varnish scripting seems rather robust for the vcl but I can't yet figure out how to make it do what I need. I run various sites from the same code base and I want a unified varnish cache for most of the directories so
x.mysite.org/theme/something.gif and y.mysite.org/theme/something.gif should not store two copies of the same gif in varnish cache
However
x.mysite.org/file.php/1 and y.mysite.org/file.php/1 should have separate caches based on the url.
Also mysite.org is a whole other site that has its own cache.
My current direction is as follows
sub vcl_fetch {
if (req.url ~ ".*\.org/file\.php") {
# do normal site specific caching
} elseif (req.url ~ "^+?\.mysite.org") {
# cache all found material in a base directory so everyone knows where to look
set req.url = regsub(req.url, "(.*\.org)(.*)", "base.mysite.org\2");
} else {
# do normal site specific caching for base site
}
}
sub vcl_recv {
# do I need to do something here to look in base.mysite.org
}
I can make base.mysite.org a real apache served site if necessary so the requests can fall through if no cache.
Am I on the write path, any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该规范化
req.http.host
而不是req.url
,因此You should normalize
req.http.host
instead ofreq.url
, so默认情况下,Varnish 将使用主机名 + URL 来获取缓存对象的哈希值。这意味着即使 x.mysite.org/theme/something.gif 和 y.mysite.org/theme/something.gif 指向完全相同的内容 Varnish 也会将它们视为两个不同的缓存对象。让它们指向同一个缓存对象的唯一方法是标准化主机名,正如 Ivy 在他的帖子中解释的那样。
'希望有帮助。
By default Varnish will use the hostname + the URL to get a hash of a cache object. It means that even if x.mysite.org/theme/something.gif and y.mysite.org/theme/something.gif point to the exact same content Varnish will see them as two different cache objects. The only way for you to make them point to the same cache object is to normalize the hostname as Ivy explained in his post.
'Hope that helps.