Skip to content

Add support to include custom VCL at the start & end of the VCL #69

Description

@peterjaap

We now have app/etc/varnish6.vcl in our Git repo, in which we update our VCL configuration.

It would be nice to keep this repo with the main VCL in here, and do modifications to the VCL through inclusion.

We would need to add a before and an after inclusion to cover all cases.

How VCL Subroutine Prepend/Append Works

  • Only one definition per VCL hook subroutine (e.g., vcl_deliver) is active in the final compiled VCL. Later definitions override earlier ones.
  • Use return to prevent fallthrough to builtin VCL logic.
  • Use prepend/append files to structure reusable logic (e.g., define shared sub blocks), but call them explicitly in the active vcl_* subroutine.
  • If composing VCLs via includes, make sure only one file defines a given vcl_* unless you understand the override order.
  • Use varnishd -C -f composed.vcl to inspect what actually gets compiled.

Examples;

# prepend.vcl
# This file is *prepended* to the main VCL during composition.
# Define or partially override VCL subroutines here.
# These definitions will be overridden if the same subroutine is defined later in the main VCL.

sub vcl_deliver {
    # This runs *before* any later vcl_deliver definitions (if they don't override this).
    set resp.http.X-Prepended = "set in prepend.vcl";

    # Important: Without return(), control will fall through to the next vcl_deliver definition
    # (e.g., in main.vcl or builtin.vcl).
    # To stop further execution, add an explicit return:
    # return (deliver);
}
# main.vcl
# The core VCL logic, included after prepend.vcl.
# Subroutine definitions here will OVERRIDE any previous ones with the same name.

include "prepend.vcl";

sub vcl_deliver {
    set resp.http.X-Main = "set in main.vcl";

    # This will replace the previous vcl_deliver unless it was only partial logic (e.g., in a called sub).
    return (deliver);
}
# append.vcl
# This file is *appended* to the main VCL.
# Useful for shared subroutines or logic you want to call from within the main subroutines.
# Avoid declaring VCL hook subroutines (like vcl_recv, vcl_deliver) here unless you are *not* redefining them elsewhere.

sub my_append_logic {
    set resp.http.X-Appended = "logic in append.vcl";
}

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions