wodby / varnish

Varnish docker container image
https://wodby.com/stacks/varnish
MIT License
59 stars 29 forks source link

Drupal Preset SESS[a-z0-9]+|SSESS[a-z0-9]+|NO_CACHE not appended #30

Closed serhat-ozkara closed 3 years ago

serhat-ozkara commented 3 years ago

If VARNISH_DRUPAL_PRESERVED_COOKIES is set, required Session and No_Cache cookies are also gets stripped contrary to documentation. Also, it's not clear how to handle multiple cookies with VARNISH_DRUPAL_PRESERVED_COOKIES on composer.yml

dockercompose: VARNISH_DRUPAL_PRESERVED_COOKIES: "example" actual varnish container "preset.vcl": `/ # cat /etc/varnish/preset.vcl sub vcl_recv {

# Pass through any administrative or AJAX-related paths.
if (req.url ~ "^(/update\.php|/([a-z]{2}/)?admin|/([a-z]{2}/)?admin/.*|/([a-z]{2}/)?system/files/.*|/([a-z]{2}/)?flag/.*|.*/ajax/.*|.*/ahah/.*)$") {
    return (pass);
}

if (req.url ~ "(^/([a-z]{2}/)?batch)") {
    return (pipe);
}

# Remove all cookies that Drupal doesn't need to know about. We explicitly
# list the ones that Drupal does need, the SESS and NO_CACHE. If, after
# running this code we find that either of these two cookies remains, we
# will pass as the page cannot be cached.
if (req.http.Cookie) {

    # 1. Append a semi-colon to the front of the cookie string.
    # 2. Remove all spaces that appear after semi-colons.
    # 3. Match the cookies we want to keep, adding the space we removed
    #    previously back. (\1) is first matching group in the regsuball.
    # 4. Remove all other cookies, identifying them by the fact that they have
    #    no space after the preceding semi-colon.
    # 5. Remove all spaces and semi-colons from the beginning and end of the
    #    cookie string.
    set req.http.Cookie = ";" + req.http.Cookie;
    set req.http.Cookie = regsuball(req.http.Cookie, "; +", ";");
    set req.http.Cookie = regsuball(req.http.Cookie, ";(example)=", "; \1=");
    set req.http.Cookie = regsuball(req.http.Cookie, ";[^ ][^;]*", "");
    set req.http.Cookie = regsuball(req.http.Cookie, "^[; ]+|[; ]+$", "");

    if (req.http.Cookie == "") {
        # If there are no remaining cookies, remove the cookie header. If there
        # aren't any cookie headers, Varnish's default behavior will be to cache
        # the page.
        unset req.http.Cookie;
    }
    else {
        # If there is any cookies left (a session or NO_CACHE cookie), do not
        # cache the page. Pass it on to Apache directly.
        return (pass);
    }
}

}`

csandanov commented 3 years ago

the default value of VARNISH_DRUPAL_PRESERVED_COOKIES is a regex that instructs what matched cookies to preserve, if current cookies match any of this, the page will not be cached