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 {
# 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)
if (beresp.status == 301 || beresp.status == 302 || beresp.status == 303) {
set beresp.http.location = regsub(beresp.http.location, 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)
if (beresp.status == 301 || beresp.status == 302 || beresp.status == 303) {
set beresp.http.location = regsub(beresp.http.location, req.http.X-get-devicetype, "");
}
}
unset req.http.X-get-devicetype;
}
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