Reputation: 13417
I'm setting up varnish-devicedetect VCL in Varnish 4.0.2:
https://github.com/varnish/varnish-devicedetect/blob/master/INSTALL.rst
I'm following the directions for method #1: "Send HTTP header to backend"
I've read through this readme and have Googled for quite some time now and still quite a few concepts are escaping me.
Here's my code (excerpts):
default.vcl
include "devicedetect.vcl";
sub vcl_recv {
call devicedetect;
# ... snip ...
}
sub vcl_backend_response {
# device detect
if (bereq.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";
}
}
# ... snip ...
}
sub vcl_deliver {
# device detect
if ((req.http.X-UA-Device) && (resp.http.Vary)) {
set resp.http.Vary = regsub(resp.http.Vary, "X-UA-Device", "User-Agent");
}
# ... snip ...
}
Here's my questions.
Vary
header set to User-Agent
. Isn't the whole approach of method #1 NOT to use user agent, and instead use X-UA-Device?call devicedetect
just looks at the User-Agent and then sets X-UA-Device
header with the appropriate grouping on the request to the backend. I'm a bit confused what the other 2 code blocks do though.X-UA-Device-force
if I don't intend to allow the user to 'use desktop site'?Rails:
def detect_device
if request.headers['X-UA-Device'] =~ /^mobile/
@device = 'mobile'
prepend_view_path Rails.root + 'app' + 'views_mobile'
else
@device = 'desktop'
end
end
Upvotes: 1
Views: 241
Reputation: 1207
As to point 1, your X-UA-Device is a custom header for internal consumption, ie by default not exposed to the external world. To ensure the external caches/proxies understand you are considering the device/user-agent in the response, you have to update the Vary with a header which reflect this. this is where the user-agent comes in, as thats where you have derived the X-UA-Device from.
note the comment within the link you indicate
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.
Upvotes: 1