如何在清漆中禁用"传输编码:分块"编码?



使用Varnish 4,我有一组响应有效Content-Length标头而没有Transfer-Encoding标头的后端。

在客户端的第一次命中,而不是用这些头响应客户端,Varnish正在删除Content-Length头并将Transfer-Encoding: chunked添加到响应中。(有趣的是,有效载荷似乎没有任何块-它是一个连续的有效载荷)。

这会导致像Flash视频播放器这样的客户端出现严重的问题,这些客户端正在尝试基于Content-Length报头进行段大小,带宽等分析。他们的分析失败了,他们不能做像多比特率流等事情。

我尝试了一些半明显的东西,比如:

  • beresp.do_stream = true
  • beresp.do_gzip = false
  • unset req.http.Accept-Encoding

示例后端响应:

HTTP/1.1 200 OK
Cache-Control: public, max-age=600
Content-Type: video/mp4
Date: Tue, 13 May 2014 19:44:35 GMT
Server: Apache
Content-Length: 796618
Connection: keep-alive

样品清漆响应:

HTTP/1.1 200 OK
Server: Apache
Cache-Control: public, max-age=600
Content-Type: video/mp4
Date: Tue, 13 May 2014 23:10:06 GMT
X-Varnish: 2
Age: 0
Transfer-Encoding: chunked
Accept-Ranges: bytes

对象的后续加载包括Content-Length头,而不是第一次加载到缓存中。

VCL: https://gist.github.com/onethumb/e64a405cc579909cace1

varnishlog输出:https://gist.github.com/onethumb/e66a2bc4727a3a5340b6

清漆痕迹:https://www.varnish-cache.org/trac/ticket/1506

目前,do_stream = false会做你想做的。

在后端发送未分块编码的情况下避免分块编码是Varnish未来可能的改进。

的例子:

sub vcl_backend_response {
        if(beresp.http.Content-Type ~ "video") {
                set beresp.do_stream = false;
                set beresp.do_gzip = false;
                //set resp.http.Content-Length = beresp.http.Content-Length;
        }
        if(beresp.http.Edge-Control == "no-store") {
                set beresp.uncacheable = true;
                set beresp.ttl = 60s;
                set beresp.http.Smug-Cacheable = "No";
                return(deliver);
        }
}

所以这个解决方案并不直观,但是您必须启用esi处理:

sub vcl_backend_response {
        set beresp.do_esi = true;
        if(beresp.http.Content-Type ~ "video") {
                set beresp.do_stream = true;
                set beresp.do_gzip = false;
                //set resp.http.Content-Length = beresp.http.Content-Length;
        }
        if(beresp.http.Edge-Control == "no-store") {
                set beresp.uncacheable = true;
                set beresp.ttl = 60s;
                set beresp.http.Smug-Cacheable = "No";
                return(deliver);
        }
}

所以我通过浏览源代码发现了这个

特别是,Varnish这样做:

if (!req->disable_esi && req->obj->esidata != NULL) {
    /* In ESI mode, we can't know the aggregate length */
    req->res_mode &= ~RES_LEN;
    req->res_mode |= RES_ESI;
}

以上代码设置了res_mode标志。

过了一会儿:

if (!(req->res_mode & (RES_LEN|RES_CHUNKED|RES_EOF))) {
    /* We havn't chosen yet, do so */
    if (!req->wantbody) {
        /* Nothing */
    } else if (req->http->protover >= 11) {
        req->res_mode |= RES_CHUNKED;
    } else {
        req->res_mode |= RES_EOF;
        req->doclose = SC_TX_EOF;
    }
}

如果HTTP协议是HTTP/1.1或更高(在您的示例中),则将res_mode标志设置为RES_CHUNKED,而res_mode标志未设置。现在甚至以后:

if (req->res_mode & RES_CHUNKED)
    http_SetHeader(req->resp, "Transfer-Encoding: chunked");

如果设置了RES_CHUNKED标志,Varnish发送分块传输编码。

only我看到的有效禁用此功能的方法是启用ESI模式。它可以通过其他几种方式禁用,但这些方法并不实用(例如,对于HTTP HEAD请求或带有304状态码的页面)。

从varnish 4.0升级到5.2,现在对于第一个请求也可以正常工作。

相关内容

最新更新