• Home
  • Varnish separate cache by user agent

Varnish separate cache by user agent

January 23, 2014 Ning 0 Comments

under the sub vcl_rev add this code

if (req.http.User-Agent ~ "(?i)ip(hone|od)") { set req.http.X-Device = "mobile-iphone"; }	
	elsif (req.http.User-Agent ~ "(?i)ipad")        { set req.http.X-Device = "tablet-ipad"; }	
	elsif (req.http.User-Agent ~ "(?i)android.*(mobile|mini)") { set req.http.X-Device = "mobile-android"; }	
	elsif (req.http.User-Agent ~ "(?i)android 3")              { set req.http.X-Device = "tablet-android"; }	
	elsif (req.http.User-Agent ~ "Opera Mobi")                  { set req.http.X-Device = "mobile-smartphone"; }	
	elsif (req.http.User-Agent ~ "(?i)android")         { set req.http.X-Device = "tablet-android"; }
	elsif (req.http.User-Agent ~ "PlayBook; U; RIM Tablet")         { set req.http.X-Device = "tablet-rim"; }
	elsif (req.http.User-Agent ~ "hp-tablet.*TouchPad")         { set req.http.X-Device = "tablet-hp"; }
	elsif (req.http.User-Agent ~ "Kindle/3")         { set req.http.X-Device = "tablet-kindle"; }	
	elsif (req.http.User-Agent ~ "Mobile.+Firefox")     { set req.http.X-Device = "mobile-firefoxos"; }
	elsif (req.http.User-Agent ~ "^HTC" ||
	    req.http.User-Agent ~ "Fennec" ||
	    req.http.User-Agent ~ "IEMobile" ||
	    req.http.User-Agent ~ "BlackBerry" ||
	    req.http.User-Agent ~ "BB10.*Mobile" ||
	    req.http.User-Agent ~ "GT-.*Build/GINGERBREAD" ||
	    req.http.User-Agent ~ "SymbianOS.*AppleWebKit") {
		set req.http.X-Device = "mobile-smartphone";
	}
	elsif (req.http.User-Agent ~ "(?i)symbian" ||
	    req.http.User-Agent ~ "(?i)^sonyericsson" ||
	    req.http.User-Agent ~ "(?i)^nokia" ||
	    req.http.User-Agent ~ "(?i)^samsung" ||
	    req.http.User-Agent ~ "(?i)^lg" ||
	    req.http.User-Agent ~ "(?i)bada" ||
	    req.http.User-Agent ~ "(?i)blazer" ||
	    req.http.User-Agent ~ "(?i)cellphone" ||
	    req.http.User-Agent ~ "(?i)iemobile" ||
	    req.http.User-Agent ~ "(?i)midp-2.0" ||
	    req.http.User-Agent ~ "(?i)u990" ||
	    req.http.User-Agent ~ "(?i)netfront" ||
	    req.http.User-Agent ~ "(?i)opera mini" ||
	    req.http.User-Agent ~ "(?i)palm" ||
	    req.http.User-Agent ~ "(?i)nintendo wii" ||
	    req.http.User-Agent ~ "(?i)playstation portable" ||
	    req.http.User-Agent ~ "(?i)portalmmm" ||
	    req.http.User-Agent ~ "(?i)proxinet" ||
	    req.http.User-Agent ~ "(?i)sonyericsson" ||
	    req.http.User-Agent ~ "(?i)symbian" ||
	    req.http.User-Agent ~ "(?i)windows\ ?ce" ||
	    req.http.User-Agent ~ "(?i)winwap" ||
	    req.http.User-Agent ~ "(?i)eudoraweb" ||
	    req.http.User-Agent ~ "(?i)htc" ||
	    req.http.User-Agent ~ "(?i)240x320" ||
	    req.http.User-Agent ~ "(?i)avantgo") {
		set req.http.X-Device = "mobile-generic";
	}

Set up a different hash for mobile

sub vcl_hash { 
if ( req.http.X-Device ~ "^mobile") {
  hash_data(req.http.X-Device);
  }
}

leave a comment

*