Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Varnish: Misc

Can Varnish Set Cookies?

 

 

Varnish misc RSS feed   Index | Next | Previous | View Threaded


travis.crowder at spechal

Nov 18, 2011, 12:36 PM

Post #1 of 6 (999 views)
Permalink
Can Varnish Set Cookies?

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

_______________________________________________
varnish-misc mailing list
varnish-misc [at] varnish-cache
https://www.varnish-cache.org/lists/mailman/listinfo/varnish-misc


roberto.fernandezcrisial at gmail

Nov 18, 2011, 12:48 PM

Post #2 of 6 (983 views)
Permalink
Re: Can Varnish Set Cookies? [In reply to]

Travis,

Have you tried with..

set req.http.Set-Cookie = "something";
set beresp.http.Set-Cookie = "something";

?

Regards,
Roberto O. FernŠndez Crisial
@rofc


On Fri, Nov 18, 2011 at 5:36 PM, Travis Crowder
<travis.crowder [at] spechal>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
>
> ______________________________**_________________
> varnish-misc mailing list
> varnish-misc [at] varnish-cache
> https://www.varnish-cache.org/**lists/mailman/listinfo/**varnish-misc<https://www.varnish-cache.org/lists/mailman/listinfo/varnish-misc>
>


jhayter at manta

Nov 18, 2011, 12:55 PM

Post #3 of 6 (983 views)
Permalink
RE: Can Varnish Set Cookies? [In reply to]

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

_______________________________________________
varnish-misc mailing list
varnish-misc [at] varnish-cache
https://www.varnish-cache.org/lists/mailman/listinfo/varnish-misc


travis.crowder at spechal

Nov 18, 2011, 9:33 PM

Post #4 of 6 (982 views)
Permalink
Re: Can Varnish Set Cookies? [In reply to]

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

}

_______________________________________________
varnish-misc mailing list
varnish-misc [at] varnish-cache
https://www.varnish-cache.org/lists/mailman/listinfo/varnish-misc


travis.crowder at spechal

Nov 18, 2011, 9:35 PM

Post #5 of 6 (978 views)
Permalink
Re: Can Varnish Set Cookies? [In reply to]

Sorry, vcl_fetch() should be vcl_recv() ... I copy/pasted only the
relevant parts and typed out the scopes. :\

On 11/18/2011 11:33 PM, Travis Crowder wrote:
> 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;
> }
>
> }
>
> _______________________________________________
> varnish-misc mailing list
> varnish-misc [at] varnish-cache
> https://www.varnish-cache.org/lists/mailman/listinfo/varnish-misc

_______________________________________________
varnish-misc mailing list
varnish-misc [at] varnish-cache
https://www.varnish-cache.org/lists/mailman/listinfo/varnish-misc


ruben at varnish-software

Nov 19, 2011, 3:33 AM

Post #6 of 6 (984 views)
Permalink
Re: Can Varnish Set Cookies? [In reply to]

On 11/18/11, Jim Hayter <jhayter [at] manta> wrote:
>
> I'm running varnish 2.0.5 but would not expect the syntax to have changed
> much.
>

* Varnish 2.0.x is no longer supported. Upgrade to the latest stable
release is always recommended:
https://www.varnish-cache.org/releases/

* There have been at least two major syntax change in Varnish that
entered 2.1 and also there are some changes in 3.0.
2.0 to 2.1 changes: http://www.dmsp.de/static/varnish/changes-2.1.0.html
2.1 to 3.0 changes:
https://www.varnish-cache.org/trac/browser/doc/changes.rst?rev=7b841df818ec169bd7b176e2fd36acbc3cbfb537


> Jim


--
Rubén Romero, Varnish Software
Phone: +47 21 98 92 62 / Mobile: +47 959 64 088 / Skype: ruben_varnish /
GTalk: <http://goo.gl/c4D6e>
*Varnish makes websites fly!*
Whitepapers <http://www.varnish-software.com/whitepapers> |
Video<http://www.youtube.com/watch?v=x7t2Sp174eI>
| Twitter <https://twitter.com/varnishsoftware> |
LinkedIn<http://www.linkedin.com/company/varnish-software>

_______________________________________________
varnish-misc mailing list
varnish-misc [at] varnish-cache
https://www.varnish-cache.org/lists/mailman/listinfo/varnish-misc

Varnish misc RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.