Can Varnish Set Cookies?

Travis Crowder travis.crowder at spechal.com
Sat Nov 19 06:33:18 CET 2011


On 11/18/2011 2:55 PM, Jim Hayter wrote:
> Travis wrote:
>   >  Sorry if this is on the ineternet somewhere, but I have yet to find it.
>
>   >  Can Varnish set a cookie?
>
>> In example, if a user on a mobile device goes to xyz.com, Varnish sees a
>> mobile user agent and redirects them to m.xyz.com via the vcl_error
>> section.  Before or after the redirect, can Varnish set a cookie without
>> the application doing so?
>> Thanks,
>> Travis
> I'm running varnish 2.0.5 but would not expect the syntax to have changed much.
> The obj part may no longer be what is used, I believe the current versions use beresp.
>
> I use the code below in vcl_fetch.
>     If (test for setting cookie) {
> 	set obj.http.Set-Cookie = "use_mobile_dev=yes; domain=.<xxxxxxxx>.com; path=/";
>     }
>
> This works for the limited case where I need it.  I don't remember the impact if any
> Set-Cookie headers already exist.
>
> Jim

Thank you!

I am using this for mobile detection in case it can help anyone else.  
It checks for either device=mobile or device=desktop in the query 
string.  It works with = or / to be diverse in case my developers decide 
to change it to use device/desktop or device/mobile in the future.

vcl_fetch(){
         # if they are accessing device/desktop, pass
         if(req.url ~ "device(=|/)desktop") {
                 if(req.http.Cookie ~ "view_type=mobile"){
                         error 306 req.http.Host;
                 }
                 return(pass);
         } elseif (req.url ~ "device(=|/)mobile" && req.http.Cookie ~ 
"view_type=desktop") {
                 error 305 req.http.host;
         }
         if(req.http.Cookie !~ "view_type" && req.http.Host !~ "^m\.") {
                 if (
                   req.http.User-Agent ~ "iPad" ||
                   req.http.User-Agent ~ "iP(hone|od)" ||
                   req.http.User-Agent ~ "Android" ||
                   req.http.User-Agent ~ "SymbianOS" ||
                   req.http.User-Agent ~ "^BlackBerry" ||
                   req.http.User-Agent ~ "^SonyEricsson" ||
                   req.http.User-Agent ~ "^Nokia" ||
                   req.http.User-Agent ~ "^SAMSUNG" ||
                   req.http.User-Agent ~ "^LG") {
                         # Varnish can't really do redirects, so we will 
error out
                         # and have the error handler do the redirect
                         error 305 req.http.host;
                 }
         }
}

vcl_error() {
         if (obj.status == 305){
                 set obj.http.Set-Cookie = "view_type=mobile; expires: 
Session; path=/; domain=." obj.response;
                 set obj.http.Location = "http://m." obj.response;
                 set obj.status = 302;
                 return(deliver);
         }
         if (obj.status == 306) {
                 set obj.http.Set-Cookie = "view_type=desktop; expires: 
Session; path=/; domain=." obj.response;
                 set obj.http.Location = "http://" obj.response;
                 set obj.status = 302;
         }

}




More information about the varnish-misc mailing list