2013-12-11 79 views
0

我用清漆3.设置为通过清漆一个cookie

运行一个Drupal 7个网站,我们有2点:正规网站(example.com)和手机网站(m.example.com)

目前的流程是,当用户通过他的移动设备键入常规站点时,他将直接重定向到移动站点。

我们希望让用户可以通过他的移动设备查看常规站点。

长话短说: 我们如何通过清漆设置cookie(它的重要性在于清漆会设置它,而不是应用程序)。

我们尝试这种代码:

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 ; 
      } 
     } 
     } 
    } 
    } 
} 

,但它不工作。 谢谢!

回答

0

您正在设置请求cookie而不是响应cookie,因此cookie被传递到后端而不是客户端。


警告:做set req.http.cookie = "xxx"

要当心,因为你将覆盖要求的cookie,你可以打破的Drupal会话/认证部分


长话短说,你会需要在vcl_fetchvcl_deliver上添加Set-Cookie标头(beresp.http.set-cookie),如下所示:

... 
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头是您的域

+0

凡在此代码触发“设置的cookie”的条件是否合理? (例如,我们希望在请求url时设置它:“http://example.com/?nombi=true”) – doron

+0

您将它添加到粘贴的代码上的'sub_vcl'上。你甚至可以在Varnish上进行设备检测,但我认为它不在这个问题的范围之内 – NITEMAN