How varnish can identify mobile users
In this example it basically finds out via regex on the user-agent tag if the user
is mobile if so it redirect to a mobile site.
sub identify_device {
unset req.http.hash-input;
set req.http.X-Device = “pc”;
if ( req.http.user-agent ~ “(.*Blackberry.*|.*BlackBerry.*|.*Blazer.*|.*Ericsson.*|.*htc.*|.*Huawei.*|.*iPhone.*|.*iPod.*|.*MobilePhone.*|.*Motorola.*|.*nokia.*|.*Novar
ra.*|.*O2.*|.*Palm.*|.*Samsung.*|.*Sanyo.*|.*Smartphone.*|.*SonyEricsson.*|.*Symbian.*|.*Toshiba.*|.*Treo.*|.*vodafone.*|.*Xda.*|^Alcatel.*|^Amoi.*|^ASUS.*|^Audiovox.*|^AU-MIC.
*|^BenQ.*|^Bird.*|^CDM.*|^DoCoMo.*|^dopod.*|^Fly.*|^Haier.*|^HP.*iPAQ.*|^i-mobile.*|^KDDI.*|^KONKA.*|^KWC.*|^Lenovo.*|^LG.*|^NEWGEN.*|^Panasonic.*|^PANTECH.*|^PG.*|^Philips.*|^
portalmmm.*|^PPC.*|^PT.*|^Qtek.*|^Sagem.*|^SCH.*|^SEC.*|^Sendo.*|^SGH.*|^Sharp.*|^SIE.*|^SoftBank.*|^SPH.*|^UTS.*|^Vertu.*|.*Opera.Mobi.*|.*Windows.CE.*|^ZTE.*)” ) {
set req.http.X-Device = “mobile”;
}
if (req.http.X-Device != “pc”) {
# IS MOBILE
set req.http.hash-input = req.http.X-Device;
}
}
sub vcl_recv {
call identify_device;
if (req.http.X-Device ~ “^mobile”) {
set req.hash += req.http.hash-input;
# Group mobile devices, otherwise we would get hundreds of cached versions
set req.hash += req.http.X-Device;
}
if(req.http.Accept-Encoding){
set req.hash += req.http.Accept-Encoding;
}
}
sub vcl_hash {
if (req.http.X-Device ~ “^mobile”) {
set req.hash += req.http.hash-input;
set req.hash += req.http.X-Device;
}
sub vcl_error {
# REDIRECT
if (obj.status == 750) {
# obj.response is the argument, originally req.url
set obj.http.Location = obj.response;
set obj.status = 302;
return (deliver);
}
Other solutions:
http://www.enrise.com/2011/02/mobile-device-detection-with-wurfl-and-varnish/
http://zamanosolutions.com/mobile-device-detection/