如何在 Varnish 中创建 default.vcl 中的自定义函数?

How to create custom function in default.vcl within Varnish?

我的 vcl 中有重复代码,我想创建自定义函数而不嵌入内联 C 代码。可能吗?

您可以像这样定义自定义子例程

sub my_subroutine {
  ... 
}

并这样称呼它:

call my_subroutine;

发件人:http://book.varnish-software.com/4.0/chapters/VCL_Basics.html

Subroutines in VCL take neither arguments nor return values. Each subroutine terminates by calling return (action), where action is a keyword that indicates the desired outcome. Subroutines may inspect and manipulate HTTP header fields and various other aspects of each request. Subroutines instruct how requests are handled.

Subroutine example:

sub pipe_if_local { if (client.ip ~ local) { return (pipe); } }

To call a subroutine, use the call keyword followed by the subroutine’s name:

call pipe_if_local;

Varnish has built-in subroutines that are hook into the Varnish workflow. These built-in subroutines are all named vcl_*. Your own subroutines cannot start their name with vcl_.