我有vcl_deliver的这种行为,它会从后端删除每个http.set-cookie。
这是我的varnishlog:
- VCL_call DELIVER
- RespUnset Set-Cookie: JSESSIONID=20E1512F59F3BA8A7BAE6AC2C10B0F66; Path=/; HttpOnly
- RespUnset Set-Cookie: OpenCmsOuFqn=/; Expires=Wed, 03-Feb-2016 13:18:41 GMT; Path=/
- RespUnset Set-Cookie: OpenCmsUserName=Admin; Expires=Wed, 03-Feb-2016 13:18:41 GMT; Path=/
- RespHeader Set-Cookie: LB=fep001; path=/;
- RespHeader X-Cache: MISS
- VCL_return deliver
我也可能看不到我发布的配置错误。 这是我的default.vcl配置文件:
vcl 4.0;
import std;
import directors;
backend fep001 {
.host = "fe1";
.port = "82";
.probe = {
.url = "/ping";
.interval = 10s;
.timeout = 1s;
.window = 1;
.threshold = 1;
.expected_response = 200;
}
}
backend fep002 {
.host = "fe2";
.port = "82";
.probe = {
.url = "/ping";
.interval = 10s;
.timeout = 1s;
.window = 1;
.threshold = 1;
.expected_response = 200;
}
}
sub vcl_init {
new cluster = directors.round_robin();
new clusterhash = directors.hash();
cluster.add_backend(fep001);
clusterhash.add_backend(fep001, 1.0);
cluster.add_backend(fep002);
clusterhash.add_backend(fep002, 1.0);
}
sub vcl_recv {
if (req.http.Cookie ~ "LB=fep[0-9]+") {
set req.backend_hint = clusterhash.backend(req.http.Cookie.LB);
} else {
set req.backend_hint = cluster.backend();
}
if (! std.healthy(req.backend_hint)) {
std.log("not healthy");
set req.backend_hint = cluster.backend();
}
if (req.http.Cookie) {
set req.http.Cookie = regsuball(req.http.Cookie, "(^|; ) *LB=[^;]+;? *", "\1");
}
if (req.method != "GET" && req.method != "HEAD") {
return(pass);
}
if (req.url ~ "^/export/.*$") {
return(hash);
}
return(pass);
}
sub vcl_backend_response {
set beresp.http.X-node = beresp.backend.name;
set beresp.http.Vary = "Accept-Encoding";
if (bereq.url ~ "^/export/.*$" && beresp.status < 400) {
set beresp.ttl = 30m;
} else {
set beresp.ttl = 0s;
}
return(deliver);
}
sub vcl_deliver {
if (obj.hits == 0 && req.http.Cookie !~ "LB=fep[0-9]+") {
set resp.http.Set-Cookie = "LB=" + resp.http.X-node + "; path=/;";
}
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT:" + obj.hits;
} else {
set resp.http.X-Cache = "MISS";
}
}
我怎样才能保留这些http标头?
谢谢
的Davide
答案 0 :(得分:2)
我终于找到了从文章Proper sticky session load balancing in Varnish
中推导出的一个解决方案似乎Varnish 4没有添加其他Set-Cookie但是覆盖它并且不会以这种方式添加这样的Varnish 3:
set resp.http.Set-Cookie = "LB=" + req.http.X-node + "; path=/;" + resp.http.Cookie;
这意味着你必须使用一些VMOD。
我添加了Cookie和标头导入:
vcl 4.0;
import std;
import directors;
import cookie;
import header;
我改变了后端选择:
cookie.parse(req.http.cookie);
if (cookie.get("LB")) {
set req.backend_hint = clusterhash.backend(cookie.get("LB"));
} else {
set req.backend_hint = cluster.backend();
}
if (! std.healthy(req.backend_hint)) {
std.log("not healthy");
set req.backend_hint = cluster.backend();
}
最后这是添加这些VMOD的主要原因:
if (obj.hits == 0 && req.http.Cookie !~ "LB=fep[0-9]+") {
header.remove(resp.http.Set-Cookie,"^LB=.*$");
header.append(resp.http.Set-Cookie,"LB=" + resp.http.X-node + "; Expires=" + cookie.format_rfc1123(now, 60m) + "; path=/;");
}
我希望这个答案会对某人有所帮助。