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