简体   繁体   English

Varnish 如何为多个域返回相同的缓存

[英]Varnish how to return same cache for multiple domains

MI have two domains pointing to the same nginx server. MI 有两个域指向同一个 nginx 服务器。 this is my setup and how I want to run my domains.这是我的设置以及我想如何运行我的域。

My problem is that When a user for example requests site1.com/page/ its cache is diffrent than site2.com/page .我的问题是,例如,当用户请求site1.com/page/ ,其缓存与site2.com/page不同。

I want both site1.com/page/ & site2.com/page/ to return the same cache so the server does not store cache twice.我希望site1.com/page/site2.com/page/都返回相同的缓存,这样服务器就不会存储缓存两次。

my vcl setup is set to 127.0.0.1我的 vcl 设置设置为127.0.0.1

backend default {
  .host = "127.0.0.1";
  .port = "8081";
}

Nginx Nginx

server {
        listen       8081;
        server_name  site1.com site2.com;

What rule or config can I add to make make varnish treat site1.com & site2.com the same?我可以添加什么规则或配置来使 make varnish 对待site1.comsite2.com相同? Basically I want varnish to ignore the hostname(domain) & cache base on the URL and other hash data.基本上我希望清漆忽略基于 URL 和其他 hash 数据的主机名(域)和缓存。

my varnish version is 4.0我的清漆版本是 4.0

The vcl_hash subroutine in VCL is responsible for creating the hash that is used to lookup objects in the cache. VCL 中的vcl_hash子程序负责创建用于在缓存中查找对象的 hash。 The Varnish built-in VCL for vcl_hash goes as follows: vcl_hash的 Varnish 内置 VCL如下:

sub vcl_hash {
    hash_data(req.url);
    if (req.http.host) {
        hash_data(req.http.host);
    } else {
        hash_data(server.ip);
    }
    return (lookup);
}

As you can see, it takes both the URL (through req.url ) and the Host header (through req.http.host and server.ip if there is no host) to create the lookup hash.如您所见,它需要 URL(通过req.url )和Host header(如果没有主机,则通过req.http.hostserver.ip )来创建查找 88351852.204088

The Host header is important here because it identifies the site that needs to be cached. Host header 在这里很重要,因为它标识需要缓存的站点。 If you're accelerating multiple websites on a single Varnish server, the Host header ensures site1.com/page and site2.com/page don't share the same cache.如果您在单个 Varnish 服务器上加速多个网站, Host header 确保site1.com/pagesite2.com/page不共享相同的缓存。

You seem to want the exact opposite.你似乎想要完全相反的东西。 This means that instead of relying on the built-in VCL, you have to specify a custom vcl_hash definition that will look like this:这意味着您不能依赖内置的 VCL,而必须指定一个自定义的vcl_hash定义,如下所示:

sub vcl_hash {
    hash_data(req.url);
    return (lookup);
}

This will share the cache between all websites and will only use the URL to identify objects in the cache.这将在所有网站之间共享缓存,并且只会使用 URL 来识别缓存中的对象。

If you want to have more security in place, you can also share the cache between sites explicitly, rather than sharing the cache for every single site.如果你想有更多的安全性,你也可以明确地在站点之间共享缓存,而不是共享每个站点的缓存。

Here's some example VCL code that only allows the cache to be shared between site1.com and site2.com :下面是一些仅允许在site1.comsite2.com之间共享缓存的示例 VCL 代码:

sub vcl_hash {
    if(req.http.host == "site1.com" || req.http.host == "site2.com") {
        hash_data(req.url);
        return (lookup);
    }
}

If the condition is not matched, no return statement is executed, which means the built-in VCL is still used.如果条件不匹配,则不执行 return 语句,这意味着仍然使用内置的 VCL。

WARNING: I noticed that you use Varnish version 4. Please do not use this version as it is end-of-life.警告:我注意到您使用的是 Varnish 版本 4。请不要使用此版本,因为它已停产。 There are no more updates happening for this version and no security fixes.此版本没有更多更新,也没有安全修复程序。 There are known security vulnerabilities for Varnish 4. Please upgrade to Varnish 6.0 LTS or one of the fresh releases. Varnish 4 存在已知的安全漏洞。请升级到 Varnish 6.0 LTS 或其中一个新版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM