我们需要通过Varnish前端caching来访问Plone站点。 我们想给这个网站两个不同的子域名www.site.com和editors.site.com。 后者将始终提供网页的非caching版本,供网站维护人员用来更新内容。
什么是最简单的方法来完成这个?
Varnish 2.x – 基于默认的Debian模板
configuration如下:
backend default { .host = "127.0.0.1"; .port = "8080"; } sub vcl_recv { if (req.http.host ~ "(www\.)?site\.com(:[0-9]+)?$") { set req.url = "/VirtualHostBase/http/www.site.com:80/LS/VirtualHostRoot" req.url; set req.backend = default; } else if (req.http.host ~ "editors.site.com(:[0-9]+)?$") { set req.url = "/VirtualHostBase/http/editors.site.com:80/LS/VirtualHostRoot" req.url; set req.backend = default; } } # Below is a commented-out copy of the default VCL logic. If you # redefine any of these subroutines, the built-in logic will be # appended to your code. # sub vcl_recv { if (req.http.x-forwarded-for) { set req.http.X-Forwarded-For = req.http.X-Forwarded-For ", " client.ip; } else { set req.http.X-Forwarded-For = client.ip; } if (req.request != "GET" && req.request != "HEAD" && req.request != "PUT" && req.request != "POST" && req.request != "TRACE" && req.request != "OPTIONS" && req.request != "DELETE") { /* Non-RFC2616 or CONNECT which is weird. */ return (pipe); } if (req.request != "GET" && req.request != "HEAD") { /* We only deal with GET and HEAD by default */ return (pass); } if (req.http.Authorization || req.http.Cookie) { /* Not cacheable by default */ return (pass); } return (lookup); } sub vcl_pipe { # Note that only the first request to the backend will have # X-Forwarded-For set. If you use X-Forwarded-For and want to # have it set for all requests, make sure to have: # set req.http.connection = "close"; # here. It is not set by default as it might break some broken web # applications, like IIS with NTLM authentication. return (pipe); } # sub vcl_pass { return (pass); } # sub vcl_hash { set req.hash += req.url; if (req.http.host) { set req.hash += req.http.host; } else { set req.hash += server.ip; } return (hash); } # sub vcl_hit { if (!obj.cacheable) { return (pass); } return (deliver); } # sub vcl_miss { return (fetch); } # sub vcl_fetch { if (!beresp.cacheable) { return (pass); } if (beresp.http.Set-Cookie) { return (pass); } return (deliver); } # sub vcl_deliver { return (deliver); } #
最简单的方法是configuration清漆完全假设www.site.com。 然后将这三行添加到您的vcl_recv的顶部。
#changed single quotes to double quotes throws error for single quotes in varnish 3.0 if (req.http.host == "editors.site.com") { return (pass); }
只是折返(通过); 在编辑器部分,所有的Varnish将不会caching这些请求。
sub vcl_recv { [... snip ...] else if (req.http.host ~ "editors.site.com(:[0-9]+)?$") { set req.url = "/VirtualHostBase/http/editors.site.com:80/LS/VirtualHostRoot" req.url; set req.backend = default; return(pass); } }
在你的vcl_recv的底部,你“返回(lookup)”,而是做一个:
“如果主机= editors.site.com,返回(通过)…. 否则返回(查找)”
如果(!req.http.host ==“editors.site.com”){
返回(lookup);
}
#默认规则在这里
返回(通过);
}
这是你的configuration编辑
sub vcl_recv {
if(req.http.x-forwarded-for){
设置req.http.X-Forwarded-For =
req.http.X-Forwarded-For“,”client.ip;
} else {
设置req.http.X-Forwarded-For = client.ip;
}
if(req.request!=“GET”&&
req.request!=“HEAD”&&
req.request!=“PUT”&&
req.request!=“POST”&&
req.request!=“TRACE”&&
req.request!=“选项”&&
req.request!=“DELETE”){
/ *非RFC2616或连接是奇怪的。 * /
返回(pipe道);
}
if(req.request!=“GET”&& req.request!=“HEAD”){
/ *我们只处理GET和HEAD默认* /
返回(通过);
}
如果(req.http.Authorization || req.http.Cookie){
/ *默认情况下不能caching* /
返回(通过);
}
如果(!req.http.host ==“editors.site.com”){
返回(lookup);
}
返回(通过);
}