Make Varnish Ignore Cookie Header Requests

By default, the default behavior does not require searching for queries containing a header Cookie. In other words, a request containing a header Cookiewill never be cached. I need to override this behavior to just ignore header requests Cookie.

Consider the following user behavior in my application:

  • The user enters the main page of the application ( /), the page must be cached, the backend returns cache control public, everything is in order, the page is cached using Varnish.
  • The user goes to the user page ( /not-cacheable), which is not cached, the backend returns cache control private. Also returns the title Set-Cookiein the response. The varnish ignores this request and the user ends the cookie. So far so good.
  • The user goes to the home page ( /), which, remember, is already cached. The problem is that the user request now contains a header Cookie. This causes Varnish to ignore the request and delegate to the server.

Deleting Cookie will not work , because when the user returns to the route /not-cacheable, he will not see his personalized page, since the heading has Cookiebeen crossed out. Instead, the backend returns a newly generated session with a new identifier in Set-Cookie.

In addition, each search request Cookiein the varnish triggered each request regarding the method or backend response for caching.

If there was some way to tell Varnish to simply ignore the header Cookie, this way I could cache requests with that header, letting the backend decide whether the request should be cached or not.

Any ideas?

+4
source share
2 answers

Just for the record, I finally came up with a VCL script that solves this problem using custom headers and restarting the request:

backend default
{
    .host = "127.0.0.1";
    .port = "8080";
}

sub vcl_recv
{
    if(req.http.X-Force-Backend)
    {
        return(pass);
    }

    if(req.http.Cookie && req.request ~ "(GET|HEAD)")
    {
        set req.http.X-Cookie = req.http.Cookie;
        remove req.http.Cookie;

        return(lookup);
    }
}

sub vcl_deliver
{
    if(resp.http.Cache-control ~ "(private|no-cache|no-store)" 
        && !req.http.X-Force-Backend
        && req.request ~ "(GET|HEAD)"
    )
    {
        set req.http.X-Force-Backend = "YES";
        set req.http.Cookie = req.http.X-Cookie;

        remove req.http.X-Cookie;

        return(restart);
    }
}

, Cookie , , . , , .

, - Symfony Varnish, : http://albertofem.com/post/symfony-varnish-and-http-practical-considerations.html

+1

, cookie vcl_recv , -, Wordpress, , , if,

sub vcl_recv {
  if (req.url !~ "^/wp-"){
    unset req.http.cookie;
  }
}
-1

Source: https://habr.com/ru/post/1535599/


All Articles