@@ -6,20 +6,19 @@ Serve the different content on the same URL
-------------------------------------------
To serve different content based on the device type, add the following VCL::
"""
include "devicedetect.vcl";
sub vcl_recv {
# only do device detection on non-static content.
if (!req.url ~ "^/[^?]+\.(jpeg|jpg|png|gif|ico|js|css|txt|gz|zip|lzma|bz2|tgz|tbz|html|htm)(\?.*|)$") {
call devicedetect;
include "devicedetect.vcl";
sub vcl_recv {
# only do device detection on non-static content.
if (!req.url ~ "^/[^?]+\.(jpeg|jpg|png|gif|ico|js|css|txt|gz|zip|lzma|bz2|tgz|tbz|html|htm)(\?.*|)$") {
call devicedetect;
}
}
}
sub vcl_hash {
# add the device classification to the hash, so clients get the correct cached object
if (req.http.X-hash-input) { hash_data(req.http.X-hash-input); }
}
"""
sub vcl_hash {
# add the device classification to the hash, so clients get the correct cached object
if (req.http.X-hash-input) { hash_data(req.http.X-hash-input); }
}
This will make a different set of cache objects based on what the client is identified as.
...
...
@@ -27,54 +26,50 @@ This will make a different set of cache objects based on what the client is iden
Different backend for mobile clients
------------------------------------
If you have a different backend that serves pages for mobile clients, or any special needs
in VCL, you can use the X-UA-Device header like this:
"""
include "devicedetect.vcl";
If you have a different backend that serves pages for mobile clients, or any special needs in VCL, you can use the X-UA-Device header like this::
backend mobile {
.host = "10.0.0.1";
.port = "80";
}
include "devicedetect.vcl";
sub vcl_recv {
# only do device detection on non-static content.
if (!req.url ~ "^/[^?]+\.(jpeg|jpg|png|gif|ico|js|css|txt|gz|zip|lzma|bz2|tgz|tbz|html|htm)(\?.*|)$") {
call devicedetect;
backend mobile {
.host = "10.0.0.1";
.port = "80";
}
if (req.http.X-UA-Device ~ "^mobile" || req.http.X-UA-device ~ "^tablet") {
set req.backend = mobile;
sub vcl_recv {
# only do device detection on non-static content.
if (!req.url ~ "^/[^?]+\.(jpeg|jpg|png|gif|ico|js|css|txt|gz|zip|lzma|bz2|tgz|tbz|html|htm)(\?.*|)$") {
call devicedetect;
}
if (req.http.X-UA-Device ~ "^mobile" || req.http.X-UA-device ~ "^tablet") {
set req.backend = mobile;
}
}
}
"""
Redirecting mobile clients
--------------------------
If you want to redirect mobile clients instead, you can use::
"""
include "devicedetect.vcl";
sub vcl_recv {
# only do device detection on non-static content.
if (!req.url ~ "^/[^?]+\.(jpeg|jpg|png|gif|ico|js|css|txt|gz|zip|lzma|bz2|tgz|tbz|html|htm)(\?.*|)$") {
call devicedetect;
}
if (req.http.X-UA-Device ~ "^mobile" || req.http.X-UA-device ~ "^tablet") {
error 750 "Moved Temporarily";
include "devicedetect.vcl";
sub vcl_recv {
# only do device detection on non-static content.
if (!req.url ~ "^/[^?]+\.(jpeg|jpg|png|gif|ico|js|css|txt|gz|zip|lzma|bz2|tgz|tbz|html|htm)(\?.*|)$") {
call devicedetect;
}
if (req.http.X-UA-Device ~ "^mobile" || req.http.X-UA-device ~ "^tablet") {
error 750 "Moved Temporarily";
}
}
}
sub vcl_error {
if (obj.status == 750) {
set obj.http.Location = "http://m.example.com/";
set obj.status = 302;
return(deliver);
sub vcl_error {
if (obj.status == 750) {
set obj.http.Location = "http://m.example.com/";
set obj.status = 302;
return(deliver);
}
}
}
"""
Signaling device type to the backend
------------------------------------
...
...
@@ -90,8 +85,7 @@ Varnish's perspective.
** Example 2: Override the User-Agent string sent
If you do not have full control, but can access the User-Agent string (think
the basic set of headers available by default for CGI scripts), you can change it
If you do not have full control, but can access the User-Agent string (think the basic set of headers available by default for CGI scripts), you can change it
into the device type.
To make sure that any caches out on the Internet doesn't cache it, a Vary header
...
...
@@ -99,19 +93,17 @@ on User-Agent must be added on the way out.
VCL code::
"""
# override the header before it is sent to the backend
sub vcl_miss { if (req.http.X-UA-Device) { set bereq.http.User-Agent = req.http.X-UA-Device; } }
sub vcl_pass { if (req.http.X-UA-Device) { set bereq.http.User-Agent = req.http.X-UA-Device; } }
# override the header before it is sent to the backend
sub vcl_miss { if (req.http.X-UA-Device) { set bereq.http.User-Agent = req.http.X-UA-Device; } }
sub vcl_pass { if (req.http.X-UA-Device) { set bereq.http.User-Agent = req.http.X-UA-Device; } }
# rewrite the response from the backend
sub vcl_fetch {
if (req.http.X-UA-Device) {
if (beresp.http.Vary) { set beresp.http.Vary = beresp.http.Vary + ", User-Agent"; }
else { set beresp.http.Vary = "User-Agent"; }
# rewrite the response from the backend
sub vcl_fetch {
if (req.http.X-UA-Device) {
if (beresp.http.Vary) { set beresp.http.Vary = beresp.http.Vary + ", User-Agent"; }
else { set beresp.http.Vary = "User-Agent"; }
}
}
}
"""
** Example 3: Add the device class as a GET query parameter
...
...
@@ -122,38 +114,36 @@ The same Vary trickery from Example 2 must be added here also.
VCL::
"""
# override the header before it is sent to the backend
sub add_get_devicetype {
if (req.http.X-UA-Device && req.method == "GET") {
unset req.http.X-get-devicetype;
if (bereq.url !~ "\?") {
set req.http.X-get-devicetype = "&devicetype=" + req.http.X-UA-Device;
} else {
set req.http.X-get-devicetype = "?devicetype=" + req.http.X-UA-Device;
# override the header before it is sent to the backend
sub add_get_devicetype {
if (req.http.X-UA-Device && req.method == "GET") {
unset req.http.X-get-devicetype;
if (bereq.url !~ "\?") {
set req.http.X-get-devicetype = "&devicetype=" + req.http.X-UA-Device;
} else {
set req.http.X-get-devicetype = "?devicetype=" + req.http.X-UA-Device;
}
set bereq.url = bereq.url + req.http.X-get-devicetype;
}
set bereq.url = bereq.url + req.http.X-get-devicetype;
}
}
sub vcl_miss { call add_get_devicetype; }
sub vcl_pass { call add_get_devicetype; }
# rewrite the response from the backend
sub vcl_fetch {
if (req.http.X-UA-Device) {
if (beresp.http.Vary) { set beresp.http.Vary = beresp.http.Vary + ", User-Agent"; }
else { set beresp.http.Vary = "User-Agent"; }
# if the backend returns a redirect (think missing trailing slash), we
# will potentially show the extra address to the client. we don't want
# that.
# if the backend reorders the get parameters, you may need to be smarter here. (? and & ordering)