我正在使用 varnish 3 运行 Drupal 7 站点。
我们有 2 个网站:常规网站 (example.com) 和移动网站 (m.example.com)
现在的流程是,当用户通过移动设备输入常规网站时,他将被直接重定向到移动网站。
我们希望用户可以选择通过移动设备查看常规网站。
长话短说: 我们如何通过 varnish 设置 cookie(重要的是 varnish 会设置它而不是应用程序)。
我们尝试了这段代码:
sub_vcl{
if (req.http.host ~ "^example\.com") {
if (req.url ~ "^/(admissions|arts|aspaka|bog|cc|chemistry|GermanHistory|humanities|institutes|lifesci|peace|public-affairs)($|/)|~") {
set req.backend = mondrian;
set req.http.host = "exmpale.com";
}
else {
if (req.http.cookie !~ "nomobi=true") {
if (req.url ~ "nomobi=true") {
set req.http.cookie = "nomobi=true";
}
else {
call devicedetect;
if (req.http.X-UA-Device ~ "^mobile" && req.url !~ "^/registration") {
set req.http.location = "http://m.example.com" + req.url;
error 750 ;
}
}
}
}
}
}
但它不起作用。 谢谢!
最佳答案
您正在设置请求 cookie 而不是响应 cookie,因此 cookie 会传递到后端而不是客户端。
警告:
做set req.http.cookie = "xxx"
时要小心因为您将覆盖请求 cookie,并且您可以破坏 Drupal 的 session /身份验证部分。
长话短说,您需要在 beresp.http.set-cookie
上添加 Set-Cookie header ( vcl_fetch
)或在 vcl_deliver
,类似:
...
if (req.http.cookie !~ "nomobi=true") {
if (beresp.http.Set-Cookie) {
set beresp.http.Set-Cookie = beresp.http.Set-Cookie + "nomobi=true; path=/; domain=your.cookie.domian.tld";
} else {
set beresp.http.Set-Cookie = "nomobi=true; path=/; domain=your.cookie.domian.tld";
}
}
...
您必须验证 Set-Cookie header 对于您的域来说是有效的。
关于cookies - 通过varnish设置cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20522335/