如何编写清漆脚本来做一些非常特别的事情

发布于 2024-12-24 21:23:43 字数 895 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忆梦 2024-12-31 21:23:43

您应该规范化 req.http.host 而不是 req.url,因此

sub vcl_fetch {
  # if it starts with /theme or /static, or contains .gif,.png etc, 
  #   then consider the host to the normalized/common host
  if (req.url ~ "^/(theme|static)" || req.url ~ "\.(gif|png)" ) {
    set req.http.host = "base.mysite.org";
    return (lookup);
  }
  # else, do non shared stuff here
}

You should normalize req.http.host instead of req.url, so

sub vcl_fetch {
  # if it starts with /theme or /static, or contains .gif,.png etc, 
  #   then consider the host to the normalized/common host
  if (req.url ~ "^/(theme|static)" || req.url ~ "\.(gif|png)" ) {
    set req.http.host = "base.mysite.org";
    return (lookup);
  }
  # else, do non shared stuff here
}
鲜肉鲜肉永远不皱 2024-12-31 21:23:43

默认情况下,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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文