Score:0

What does this block of varnish code do?

in flag

I have this code in varnish config and not sure what it do! This config will cache or not my client requests? what is wrong with it?

sub vcl_backend_response {
    if (beresp.status != 200) {
        return (pass);
    }
    set beresp.http.X-Backend = beresp.backend.name;


    unset beresp.http.cookie;
    unset beresp.http.Set-Cookie;

    if (bereq.http.x-render-type == "test" && beresp.http.Content-Type ~ "text/html") {
        set beresp.http.Cache-Control = "no-store";
    }

    set beresp.http.Cache-Control = "no-store";
    if (bereq.http.x-render-type == "test" && beresp.http.Content-Type ~ "text/html") {
        return (pass);
    }

    return (deliver);
}
Score:0
in flag

This block of VCL code seems to specify some rules on when to bypass the cache. However, the way it is written doesn't make a lot of sense.

Bypassing the cache

return(pass) is not the proper way of bypassing the caching in the vcl_backend_response context. return(pass) is used in vcl_recv when an incoming request bypasses the cache.

In vcl_backend_response bypassing the cache means preventing from storing the incoming object in the cache. The best practices dictate that you do set beresp.uncacheable = true, assign a TTL and then return(deliver). This ensures that this object bypasses the cache for a certain amount of time until the next backend response meets the required criteria.

My enabling beresp.uncacheable, you're ensuring that the object ends up on the waiting list and becomes a candidate for request coalescing.

Removing cookies

Removing cookies often makes sense to improve your hit rate. In the backend context you will remove the Set-Cookie header. This is correctly done in vcl_backend_response through unset beresp.http.Set-Cookie, however this is done unconditionally.

This means that any Set-Cookie action will not take place, which could result in inconsistent behavior. Not sure if pre-conditions are required for the removal of these cookies.

You can also remove incoming cookies through unset req.http.Cookie. But there seems to be a similar call in vcl_backend_response that runs unset beresp.http.Cookie.

This would indicate that a Cookie response header would be received. That seems unlikely.

Rewriting the VCL

This is how I would rewrite this VCL code without any other context:

vcl 4.1;

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

sub vcl_recv {
    unset req.http.Cookie;
}

sub vcl_backend_response {
    if(beresp.status != 200) {
        set beresp.ttl = 120s;
        set beresp.uncacheable = true;
        return(deliver);
    }

    set beresp.http.X-Backend = beresp.backend.name;
    unset beresp.http.Set-Cookie;
    set beresp.http.Cache-Control = "no-store";

    if (bereq.http.x-render-type == "test" && beresp.http.Content-Type ~ "text/html") {
        set beresp.ttl = 120s;
        set beresp.uncacheable = true;
        return(deliver);
    }

    return (deliver);
}

Warning: I would not recommend copy/pasting this could into your production environment. I have a feeling that some corners were cut when writing this VCL. Since I don't have any additional context, I don't want to advise using this.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.