Skip to content

Instantly share code, notes, and snippets.

@marcofbb
Last active December 28, 2021 12:24
Show Gist options
  • Save marcofbb/99b7c1fdc14d2a262844a9d20d466ff0 to your computer and use it in GitHub Desktop.
Save marcofbb/99b7c1fdc14d2a262844a9d20d466ff0 to your computer and use it in GitHub Desktop.
Varnish cache different for Mobile / PC/ Tablet
## ## Not complete default.vcl code
# Routine to try and identify device
sub identify_device {
# Default to thinking it's desktop
set req.http.X-UA-Device = "desktop";
if (req.http.User-Agent ~ "iPad" ) {
# It says its a iPad - so let's give them the tablet-site
set req.http.X-UA-Device = "tablet";
}
else if (req.http.User-Agent ~ "iP(hone|od)" || req.http.User-Agent ~ "Android" || req.http.User-Agent ~ "Windows Phone" || req.http.User-Agent ~ "[ (]BlackBerry;") {
# It says its a iPhone, iPod, Android or BlackBerry - so let's give them the touch-site..
set req.http.X-UA-Device = "smartphone";
}
else if (req.http.User-Agent ~ "Kindle" || req.http.User-Agent ~ "^nook browser") {
set req.http.X-UA-Device = "ereader";
}
else if (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" || req.http.User-Agent ~ "IEMobile" || req.http.User-Agent ~ "Windows CE") {
# Some other sort of mobile
set req.http.X-UA-Device = "mobile-other";
}
}
sub vcl_recv {
call identify_device;
}
sub vcl_hash {
hash_data(req.url);
if (req.http.X-UA-Device){
# Add device info with uniqueID cache
hash_data(req.http.X-UA-Device);
}
if (req.http.host) {
hash_data(req.http.host);
} else {
hash_data(server.ip);
}
return (lookup);
}
@marcofbb
Copy link
Author

marcofbb commented Jul 7, 2021

@fatihtoprak

its says syntax error

Ready fixed

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment