Tallboy
Tallboy

Reputation: 13417

Several questions about this Varnish VCL

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.

  1. When I inspect the response in Chrome Dev Tools, why is the 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?
  2. Based on other guides I read... it seems this will hit the origin for EACH type of mobile (if you look in device detect, its split up into... mobile-iphone, mobile-android, mobile-smartphone, etc). Is this true in my code above? I definitely DONT want to hit the origin server more than twice for any given URL (desktop, and mobile ... I don't want all the mobile-* cached separately).
  3. Can someone describe what the 3 code blocks above actually do? In somewhat laymen's terms. About the only one I truly understand is the first code block. 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.
  4. Can I delete the bit with X-UA-Device-force if I don't intend to allow the user to 'use desktop site'?
  5. The guide mentions that I should be setting something in the backend in my app code. Right now this is all I have (rails). I'm not changing headers or changing anything about the response. I'm only changing the way the HTML looks (for the mobile version of the site). Should I be changing a header or something? This is what I have so far:

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

Answers (1)

Marcel Dumont
Marcel Dumont

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

Related Questions