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 ...@@ -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) if (beresp.status == 301 || beresp.status == 302 || beresp.status == 303) {
if (beresp.status == 301 || beresp.status == 302 || beresp.status == 303) { set beresp.http.location = regsub(beresp.http.location, req.http.X-get-devicetype, "");
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. ...@@ -165,11 +155,13 @@ There are some tools included for testing and validating your setup.
* backend/example-backend.py * backend/example-backend.py
* devicedetect-dev.vcl * 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 If you include the -dev.vcl file, you can access /set_ua_device/ to set a
which is sent to the backend. (and used for cache lookups) 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 Example: enable devicedetection, go to /set_ua_device/mobile-iphone .
get the content as if your browser was an iPhone. Watch out for the TTL settings. 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 There is an example web server in backend/ that listens on port 5911 and replies
differently depending on X-UA-Device. Run it with: differently depending on X-UA-Device. Run it with:
...@@ -179,8 +171,7 @@ 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: Now you can access it through:
http://localhost:5911/devicetest/ http://localhost:5911/devicetest/ , or
OR http://localhost:6081/devicetest/ # Change 6081 into your Varnish listening port.
http://localhost:6081/devicetest/ # exchange 6081 for your varnish listening port.
Happy devicedetecting. 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