2017-06-28 19 views
1

在我的Firefox或Chrome浏览器中,如果检查HTTP标头,结果始终是Content-Encoding:gzip。但我有客户报告他们看到“传输编码:分块”,而不是解压缩请求。CSS并不总是被压缩为什么?

http://www.example.com/public/css/style.min.css

如果我或者客户做一个gzip压缩在网上查询它的证实gzip的活跃。

https://checkgzipcompression.com = gzip!

但是,如果我使用这样的检查。 http://onlinecurl.com/

我也得到传输编码的:分块

请求:

GET /style/css.css HTTP/1.1 
Host: www.example.com 
Connection: keep-alive 
Pragma: no-cache 
Cache-Control: no-cache 
User-Agent: ... 
Accept:/
Referer: http://www.example.com/ 
Accept-Encoding: gzip, deflate 
Accept-Language: ... 
Cookie: ... 

响应:

HTTP/1.1 200 OK 
Age: 532948 
cache-control: public, max-age=604800 
Content-Type: text/css 
Date: Wed, 28 Jun 2017 12:35:07 GMT 
ETag: "5349e8d595dfd21:0" 
Last-Modified: Wed, 07 Jun 2017 13:56:17 GMT 
Server: Microsoft-IIS/7.5 
Vary: X-UA,Accept-Encoding, User-Agent 
X-Cache: HIT 
X-Cache-Hits: 6327 
X-CacheReason: Static-js-css. 
X-Powered-By: ASP.NET 
X-Served-By: ip-xxx-xxx-xxx-xx.name.xxx 
x-stale: true 
X-UA-Device: pc 
X-Varnish: 993020034 905795837 
X-Varnish-beresp-grace: 43200.000 
X-Varnish-beresp-status: 200 
X-Varnish-beresp-ttl: 604800.000 
transfer-encoding: chunked 
Connection: keep-alive 

为什么有些要求不gzip压缩,当它应该,这是我的Varnish配置(与gzip相关的部分):

if (req.http.Accept-Encoding) { 
    if (req.url ~ "\.(jpg|png|gif|gz|tgz|bz2|tbz|mp3|ogg|flv|swf)$") { 
    # No point in compressing these 
    remove req.http.Accept-Encoding; 
    } elsif (req.http.Accept-Encoding ~ "gzip") { 
    set req.http.Accept-Encoding = "gzip"; 
} elsif (req.http.Accept-Encoding ~ "deflate") { 
    set req.http.Accept-Encoding = "deflate"; 
    } else { 
    # unkown algorithm 
    remove req.http.Accept-Encoding; 
    } 
} 

# Enabling GZIP 
if (beresp.http.Content-Type ~ "(text/css|application/x-javascript|application/javascript)") { 
    set beresp.do_gzip = true; 
} 


if (beresp.http.Content-Encoding ~ "gzip") { 
    if (beresp.http.Content-Length == "0") { 
    unset beresp.http.Content-Encoding; 
    } 
} 

set beresp.http.Vary = regsub(beresp.http.Vary, "(?i)^(.*?)X-Forwarded-URI,?(.*)$", "\1\2"); 
set beresp.http.Vary = regsub(beresp.http.Vary, "(?i)^(.*?)User-Agent,?(.*)$", "\1\2"); 
set beresp.http.Vary = regsub(beresp.http.Vary, "^(.*?),?$", "X-UA,\1"); 
set beresp.http.Vary = regsub(beresp.http.Vary, "^(.*?),?$", "\1"); 

任何想法,谢谢。

回答

2

如果请求表明它可以接受gzip压缩的响应,则只有gzip响应。这由请求中的Accept-Encoding标题指示。所以也许你的在线curl没有发送这个头文件。对于看到这个的客户可能是一样的。你真的有客户报告他们没有得到gzip响应?

更新

啊,我看你现在在做什么。你使用最近版本的清漆吗?现在不需要自己做所有这些。清漆在本地处理。您只需将do_gzip设置为on即可获取您想要的内容类型,然后Varnish负责其余的部分,包括Accept-Encoding标题。请参阅the documentation here

所以只要删除所有相关的代码,除了部分您的gzip /编码的直属# Enabling GZIP

# Enabling GZIP 
if (beresp.http.Content-Type ~ "(text/css|application/x-javascript|application/javascript)") { 
    set beresp.do_gzip = true; 
} 

这可能会得到一切工作。这对我来说很好。 VCL的最佳用量越少越好,Varnish本身就擅长处理事物。不要忘记在更改后重新启动Varnish或清除此网站的缓存。

在情况下,它是非常有用的,我用下面的VCL此:

if (
    beresp.status == 200 
    && beresp.http.content-type ~ "\b((text/(html|plain|css|javascript|xml|xsl))|(application/(javascript|xml|xhtml\+xml)))\b" 
) { 
    set beresp.do_gzip = true; 
} 

这对于可从压缩获益更多的内容类型,包括HTML检查。我不打扰与application/x-javascript,因为它是古老的,不使用。

另一个说明,你确定你需要修改Vary标题的方式,你在那里做?

+0

我的请求包括接受编码:gzip,deflate,但响应仍然没有gzip和Transfer-Encoding:chunked – steffanjj

+0

我认为最好是发布所有的VCL。 – SuperDuperApps

+0

使用与gzip相关的部分更新了帖子。让我知道你是否需要完整的VCL。谢谢。 – steffanjj