Commit a906e742 authored by Lasse Karstensen's avatar Lasse Karstensen

Rework format a bit

parent 822bc820
......@@ -5,79 +5,25 @@ Install and configure
Serve the different content on the same URL
-------------------------------------------
To serve different content based on the device type, add the following VCL::
Here are a few examples on how to serve different content to the client based on device classification.
include "devicedetect.vcl";
sub vcl_recv { 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); }
}
This will make a different set of cache objects based on what the client is identified as.
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";
backend mobile {
.host = "10.0.0.1";
.port = "80";
}
The tricks involved are:
1. Detect the client (pretty simple, just include devicedetect.vcl and call it)
2. Figure out how to signal the backend what client class this is. This includes for example setting a header, changing a header or even changing the backend request URL.
3. Modify any response from the backend to add missing Vary headers, so Varnish' internal handling of this kicks in.
4. Modify output sent to the client so any caches outside our control don't serve the wrong content.
sub vcl_recv {
call devicedetect;
All this while still making sure that we only get 1 cache object per URL per device class.
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 you can use the following snippet.
.. 065-redir-mobile-start
VCL::
include "devicedetect.vcl";
sub vcl_recv {
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" + req.url;
set obj.status = 302;
return(deliver);
}
}
.. 065-redir-mobile-end
Signaling device type to the backend
------------------------------------
Except where redirection is used, the backend needs to be told what kind of
client the content is meant to be served to.
Example 1: Send HTTP header to backend
''''''''''''''''''''''''''''''''''''''
The basic case is that Varnish add the X-UA-Device HTTP header on the
backend requests, and the backend mentions in the response Vary header that the
content is dependant on this header. Everything works out of the box from
Varnish's perspective.
content is dependant on this header.
Everything works out of the box from Varnish' perspective.
.. 071-example1-start
Example VCL::
......@@ -95,10 +41,8 @@ Example VCL::
# completed.
sub vcl_miss { call add_x-ua-device; }
sub vcl_pass { call add_x-ua-device; }
.. 071-example1-end
Please remember that the backend must send a Vary header on User-Agent, or you will need to add that manually. See below for an example.
.. 071-example1-end
Example 2: Normalize the User-Agent string
''''''''''''''''''''''''''''''''''''''''''
......@@ -125,44 +69,17 @@ VCL::
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; } }
# so, this is a bit conterintuitive. The backend creates content based on the normalized User-Agent,
# but we use Vary on X-UA-Device so Varnish will use the same cached object for all U-As that map to
# the same X-UA-Device.
# if the backend does not mention in Vary that it has crafted special
# content based on the User-Agent (==X-UA-Device), add it.
# If your backend does set Vary: User-Agent, you may have to remove that here.
sub vcl_fetch {
if (req.http.X-UA-Device) {
if (!beresp.http.Vary) { # no Vary at all
set beresp.http.Vary = "X-UA-Device";
} elseif (beresp.http.Vary !~ "X-UA-Device") { # add to existing Vary
set beresp.http.Vary = beresp.http.Vary + ", X-UA-Device";
}
}
# comment this out if you don't want the client to know your classification
set beresp.http.X-UA-Device = req.http.X-UA-Device;
}
# to keep any caches in the wild from serving wrong content to client #2 behind them, we need to
# transform the Vary on the way out.
sub vcl_deliver {
if ((req.http.X-UA-Device) && (resp.http.Vary)) {
set resp.http.Vary = regsub(resp.http.Vary, "X-UA-Device", "User-Agent");
}
}
.. 072-example2-end
Example 3: Add the device class as a GET query parameter
''''''''''''''''''''''''''''''''''''''''''''''''''''''''
If everything else fails, you can add the device type as a GET argument.
If everything else fails, you can add the device type as a GET argument.
http://example.com/article/1234.html --> http://example.com/article/1234.html?devicetype=mobile-iphone
The same Vary trickery from Example 2 must be added here also.
The client itself does not see this classification, only the backend request is changed.
.. 073-example3-start
VCL::
......@@ -202,6 +119,54 @@ VCL::
.. 073-example3-end
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";
backend mobile {
.host = "10.0.0.1";
.port = "80";
}
sub vcl_recv {
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 you can use the following snippet.
.. 065-redir-mobile-start
VCL::
include "devicedetect.vcl";
sub vcl_recv {
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" + req.url;
set obj.status = 302;
return(deliver);
}
}
.. 065-redir-mobile-end
Testing tools
-------------
......@@ -217,7 +182,7 @@ cookie that overrides the value of X-UA-Device which is sent to the backend.
Example: enable devicedetection, go to /set_ua_device/mobile-iphone .
Afterwards, access your site as usual. You will now get the content as if your
browser was an iPhone. Watch out for the TTL settings.
browser was an iPhone.
There is an example web server in backend/ that listens on port 5911 and replies
differently depending on X-UA-Device. Run it with::
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment