cookies - 通过varnish设置cookie

标签 cookies drupal-7 varnish httpcookie varnish-vcl

我正在使用 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/

相关文章:

php - Drupal - 以编程方式更改某些迁移术语的 url 别名

php - 匿名用户的 Drupal 权限

phpmyadmin - 安装 Varnish 后无法访问 phpMyAdmin

restart - Varnish 重启跟踪

symfony - 如何使用 Oauth2 协议(protocol)将 varnish 与 RESTful Api 结合使用?

javascript - 单击按钮时删除两个 cookie

javascript - 从自定义元素重新加载 polymer 元素或 ajax 调用

javascript - 在页面关闭时保存动态变量

jquery - 无法读取 cookie(使用 jQuery 和 cookies 插件)

node.js - Drupal 7. Bootstrap 和 Grunt