Same URL: Don't cache with store response header, but cache when it's not there

Guillaume Quintard guillaume at varnish-software.com
Mon Jan 21 03:39:14 UTC 2019


Hi,

> Is it because I have one url -
> - that can return 2 responses based on browser

That's the one, you need to tell varnish to care about the browser. You
have two ways to do that: either put that in in the hash key directly, or
put it in in a header, and tell that to varnish via the vary header.

First option looks like this:

sub vcl_recv {
  if (SOME_TEST_TO_CHECK_ITS_SAFARI) {
      set req.http.browser = "safari";
  } else {
    set req.http.browser = "somethingelse";
  }
}

sub vcl_hash {
  hash_data(req.http.browser);
  # do NOT return, let the built-in vcl run
}

The pro is that you don't need to change the backend, the con is that if
you purge, you have to purge twice because they are two different objects.

The second option is to tell varnish that the browser is important, but
just a variant of the same object:

sub vcl_recv {
  if (SOME_TEST_TO_CHECK_ITS_SAFARI) {
      set req.http.browser = "safari";
  } else {
    set req.http.browser = "somethingelse";
  }
}

sub vcl_backend_response {
  set beresp.http.vary = beresp.http.vary + ",browser";
}

Note that you can also have the backend return the vary header for you
directly

There are a couple of cleaner versions, but they are a bit more involved,
so let's start with that.

Side note: I understand where you are coming from with the std.log
debugging, but that info is actually super redundant with what's in the log.

-- 
Guillaume Quintard


On Sun, Jan 20, 2019 at 4:14 AM Maninder Singh <mandys at gmail.com> wrote:

> Hi Everyone,
>
> I am unable to get caching to work in a particular scenario.
>
> I need to cache the url eg: /app/profile/GDPR1?hostingUrl=http%3A%2F%
> 2Fitvidffya.blogspot.com%2F2019%2F01%2Fvar-nish-issue-fix.html
>
> However, this is embedded in a 3rd party site (www.anotherdomain.com
> calls mydomain.com in iframe ) so on Apple Safari we can't drop cookies (
> as 3rd party cookies are blocked by default ).
>
> As a result, for Apple Safari, when this url is hit, our backend returns
> top.location.href='someurlonoursite_to_set_cookie' which is fired in
> context of 3rd party site since they embed using our javascript.
>
> This way a cookie is dropped on our domain and user gets back to the url
> inside the iframe.
>
> Now, when I hit this url in chrome, it always picks up
> top.location.href='....' as it got cached by safari.
>
> But, chrome was supposed to get the actual page content since it's not
> blocking 3rd party cookies.
>
> So, I went ahead and added a custom header, "store" for cases of apple
> safari in our backend.
> I skip unsetting cookie (hence varnish cache) for this url in this case.
>
> But, it still doesn't cache on chrome in subsequent hits.
>
> Always goes to backend, never goes to the cache.
>
> Is it because I have one url -
> - that can return 2 responses based on browser
> - I told it to not cache first time when store header was there, but when
> store header is not there i ask it to cache it.
>
> Still doesn't work.
>
> Config
>
> sub vcl_recv {
>     std.log("vtglog: in vcl_recv " + req.url);
>     # Only cache GET or HEAD requests. This makes sure the POST requests
> are always passed.
>     if (req.method != "GET" && req.method != "HEAD") {
>         return (pass);
>     }
>
>     if(req.http.host == "sqa03.mydomain.com" && req.url ~ "/app/profile"){
>         std.log("vtglog: unsetting cookies");
>         unset req.http.cookie;
>     } else{
>         return(pass);
>     }
>
>     if (req.http.Authorization) {
>         # Not cacheable by default
>         return (pass);
>     }
> }
>
> sub vcl_backend_response {
>     std.log("vtglog: vcl_backend_response" + bereq.url) ;
>     # Happens after we have read the response headers from the backend.
>     #
>     # Here you clean the response headers, removing silly Set-Cookie
> headers
>     # and other mistakes your backend does.
>     if (bereq.http.host == "sqa03.mydomain.com" && bereq.url ~
> "/app/profile") {
>         std.log("vtglog: inside condition in backend response");
>         std.log("vtglog: store header value is " + beresp.http.store);
>         if ( beresp.http.Set-Cookie ) {
>             if ( !beresp.http.store ) {
>                 std.log("vtglog: since no store headers, cache it by
> unsetting cookies");
>                 unset beresp.http.Set-Cookie;
>             } else {
>                 std.log("vtglog: store header found, dont cache");
>             }
>         }
>     }
>
>     if (beresp.status == 301 || beresp.status == 302) {
>         set beresp.http.Location = regsub(beresp.http.Location, ":[0-9]+",
> "");
>     }
>
>     # Don't cache 50x responses
>     if (beresp.status == 500 || beresp.status == 502 || beresp.status ==
> 503 || beresp.status == 504) {
>         return (abandon);
>     }
> }
> _______________________________________________
> varnish-misc mailing list
> varnish-misc at varnish-cache.org
> https://www.varnish-cache.org/lists/mailman/listinfo/varnish-misc
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.varnish-cache.org/lists/pipermail/varnish-misc/attachments/20190120/40fa8ad4/attachment-0001.html>


More information about the varnish-misc mailing list