清漆不是gzip html页面

时间:2015-01-07 16:18:46

标签: http tomcat compression gzip varnish

我在CentOS 6上运行多个tomcat 7.0.56,在另一个服务器Centos上运行Varnish 4。 Varnish必须为我们做两件重要的事情:成为一个反向代理(像魅力一样工作)并压缩所有可以压缩的数据。我们不关心我们架构中的缓存。 关于第二点,我们遇到了问题。清漆gzip CSS和JS,不要使用gzip html。 在我的default.vcl中,我不压缩文件,如图片,swf或我为移动设计的页面,我为所有其他东西设置了beresp.do_gzip为真。

我的 vcl_recv

sub vcl_recv { if (req.http.Accept-Encoding) { if (req.url ~ "\.(jpg|png|gif|gz|tgz|bz2|tbz|mp3|ogg|swf)$" || req.url ~ "Mobile\.") { unset req.http.Accept-Encoding; } elsif (req.http.Accept-Encoding ~ "gzip") { set req.http.Accept-Encoding = "gzip"; } elsif (req.http.Accept-Encoding ~ "deflate" && req.http.user-agent !~ "MSIE") { set req.http.Accept-Encoding = "deflate"; } else { # unkown algorithm unset req.http.Accept-Encoding; } } set req.backend_hint = h.backend(client.identity); }

我的 vcl_backend_response

sub vcl_backend_response {

 if (beresp.http.url ~ "\.(jpg|png|gif|gz|tgz|bz2|tbz|mp3|ogg|swf)$" || beresp.http.url ~ "Mobile\.") {
    set beresp.do_gzip = false;
}
else {
    set beresp.do_gzip = true;
    set beresp.http.X-Cache = "ZIP";
}}

除了html页面之外,所有通过Varnish传递的流都被正确gzip压缩。但这些页面的标题几乎是正确的。

请求标题

Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 Accept-Encoding:gzip, deflate, sdch Accept-Language:fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4 Cache-Control:no-cache Connection:keep-alive Cookie:JSESSIONID=F116C2729E96D2150EEEACEB90F95EA9.node1; UUID=631a2947-14ac4e00ca6-0233de72a654bb34bce4a88d9e172e25 Host:tomcat.domain.tld Pragma:no-cache Referer:http://tomcat.domain.tld/path/to/ServletControl?sourceview=liste_menu User-Agent:Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36

响应标题

Accept-Ranges:bytes Age:0 Cache-Control:no-store,no-cache Connection:keep-alive Content-Language:fr-FR Content-Type:text/html;charset=UTF-8 Date:Wed, 07 Jan 2015 14:55:04 GMT Expires:0 MII:1800 Pragma:no-store,no-cache Server:Apache-Coyote/1.1 Set-Cookie:UUID=631a2947-...e25; Version=1; Max-Age=10000; Expires=Wed, 07-Jan-2015 17:41:44 GMT; Path=/gce162 Transfer-Encoding:chunked Vary:Accept-Encoding Via:1.1 varnish-v4 X-Cache:ZIP X-Varnish:163870

我们可以看到标签X-Cache的值为ZIP,标签为Vary,包含accept-Encoding但没有Content-Encoding" gzip"。 所以我不明白为什么清漆不是gzip html并写下标签Vary = accept-Encoding?

欢迎任何帮助。谢谢。

巴杜

1 个答案:

答案 0 :(得分:0)

相信它与Transfer-Encoding:chunked响应标头相关,因为没有为html文件设置内容长度。

要禁用分块响应,请尝试在vcl_backend_response的else循环中添加set beresp.do_esi = true;

另见

https://www.varnish-cache.org/trac/ticket/1506How do I disable 'Transfer-Encoding: chunked' encoding in Varnish?