Skip to content

Instantly share code, notes, and snippets.

@r115
Forked from elidickinson/mobile-detect.vcl
Last active August 29, 2015 14:12
Show Gist options
  • Save r115/4f30bf76a05acc58be8d to your computer and use it in GitHub Desktop.
Save r115/4f30bf76a05acc58be8d to your computer and use it in GitHub Desktop.
# This file has been modified by Eli Dickinson to update detection rules and to
# send a Vary header downstream. It has also been updated to
# work with newer versions of Varnish.
#
# A simple mobile device detection implementation in VCL
# http://fangel.github.com/mobile-detection-varnish-drupal
#
# The file is based upon initial work done by Audun Ytterdal, which can be
# found at
# http://www.varnish-cache.org/lists/pipermail/varnish-misc/2010-April/004103.html
#
# Usage:
# Include in the top of your existing Varnish configuration like so:
# include "/path/to/device-detect.vcl";
#
# Author: Morten Fangel <fangel@sevengoslings.net>
# License: MIT License
#
# Copyright (c) 2011 Morten Fangel
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# Call the Identify Device subroutine in recv
sub vcl_recv {
if (req.url ~ "\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|pdf|txt|tar|wav|bmp|rtf|js|flv|swf)$") {
set req.http.X-Device = "na";
} else {
call identify_device;
}
}
# Routine to try and identify device
sub identify_device {
# Default to thinking it's desktop
set req.http.X-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-Device = "tablet";
}
elsif (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-Device = "smartphone";
}
# It says its a iPhone, iPod or Android - so let's give them the touch-site..
set req.http.X-Device = "smartphone";
}
elsif (req.http.User-Agent ~ "Kindle" || req.http.User-Agent ~ "^nook browser") {
set req.http.X-Device = "ereader";
}
elsif (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-Device = "mobile-other";
}
}
# # Tried adding X-Device to the Vary header rather than the Hash, but it didn't work...
#sub vcl_fetch {
# # expose X-Device to user
# set beresp.http.X-Device = req.http.X-Device;
#
# if (beresp.http.Vary) {
# set beresp.http.Vary = beresp.http.Vary + "," + "X-Device";
# } else {
# set beresp.http.Vary = "X-Device";
# }
#}
# tell any downstream caches that User Agent matters
# We add it to headers here rather than vcl_fetch because we don't want Varnish to vary by
# user agent. We are already varying by X-Device which is based on UA.
sub vcl_deliver {
if (req.http.X-Device != "na") {
if (resp.http.Vary) {
set resp.http.Vary = resp.http.Vary + "," + "User-Agent";
} else {
set resp.http.Vary = "User-Agent";
}
}
}
# Add the device to the hash (if its a mobile device)
sub vcl_hash {
hash_data(req.http.X-Device);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment