【问题标题】:Using C to modify response in VCL在 VCL 中使用 C 修改响应
【发布时间】:2020-05-23 13:56:12
【问题描述】:

varnish docs 说我们可以在 VCL 文件中包含 C sn-ps,比如

sub vcl_hash {
    C{
            int i = /* Some logic to get a number */
    }C
}

但是现在如何使用整数i 的值来设置响应标头或cookie

【问题讨论】:

    标签: c varnish-vcl varnish-4


    【解决方案1】:

    varnish.vcc

    以及功能:

    在 varnish 4 中为上下文定义了 ctx 变量(与 varnish 3 中的 sp 相反)(source)

    示例:

    sub vcl_hash {
        C{
            const char *hash = calc_hash(...);
            const struct gethdr_s hdr = { 
                HDR_BERESP,
                "\010X-Hash:" // length prefixed string, in octal 
            };
            VRT_SetHdr(ctx, &hdr, hash, vrt_magic_string_end);
        }C
    }
    

    here for another example

    【讨论】:

      【解决方案2】:

      为什么不直接使用 VCL? set resp.http.x-header = header 设置您想要设置的任何标题。

      【讨论】:

      • 因为我有一些复杂的逻辑来使用循环和条件获取变量。我不认为我们可以在 VCL 中使用循环
      【解决方案3】:

      我鼓励你直接写一个 vmod,这样会更舒服。您可以在此处找到(旧但仍然相关的)指南:https://info.varnish-software.com/blog/creating-a-vmod-vmod-str

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-10-28
        • 2011-10-27
        • 2014-09-16
        • 2020-08-03
        • 1970-01-01
        • 2010-12-03
        • 1970-01-01
        相关资源
        最近更新 更多