Commit 043b0d6a authored by Lasse Karstensen's avatar Lasse Karstensen

Clean up formatting to be correct RST

parent 735fa07c
......@@ -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 {
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 {
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,18 +26,16 @@ 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:
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";
include "devicedetect.vcl";
backend mobile {
backend mobile {
.host = "10.0.0.1";
.port = "80";
}
}
sub vcl_recv {
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;
......@@ -47,16 +44,15 @@ sub vcl_recv {
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 {
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;
......@@ -65,16 +61,15 @@ sub vcl_recv {
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) {
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 {
# 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,9 +114,8 @@ 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 {
# 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 !~ "\?") {
......@@ -134,12 +125,12 @@ sub add_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; }
}
sub vcl_miss { call add_get_devicetype; }
sub vcl_pass { call add_get_devicetype; }
# rewrite the response from the backend
sub vcl_fetch {
# 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"; }
......@@ -152,8 +143,7 @@ sub vcl_fetch {
}
}
unset req.http.X-get-devicetype;
}
"""
}
......@@ -165,11 +155,13 @@ There are some tools included for testing and validating your setup.
* backend/example-backend.py
* devicedetect-dev.vcl
If you include the -dev.vcl file, you can access /set_ua_device/ to set a cookie that overrides the value of X-UA-Device
which is sent to the backend. (and used for cache lookups)
If you include the -dev.vcl file, you can access /set_ua_device/ to set a
cookie that overrides the value of X-UA-Device which is sent to the backend.
(and used for cache lookups)
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.
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.
There is an example web server in backend/ that listens on port 5911 and replies
differently depending on X-UA-Device. Run it with:
......@@ -179,8 +171,7 @@ differently depending on X-UA-Device. Run it with:
Now you can access it through:
http://localhost:5911/devicetest/
OR
http://localhost:6081/devicetest/ # exchange 6081 for your varnish listening port.
http://localhost:5911/devicetest/ , or
http://localhost:6081/devicetest/ # Change 6081 into your Varnish listening port.
Happy devicedetecting.
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