在我的Firefox或Chrome浏览器中,如果我检查HTTP header ,则结果始终使用Content-Encoding:gzip。但是我有客户报告说,他们看到的是“传输编码:分块”,并且请求没有被压缩。
http://www.example.com/public/css/style.min.css
如果我或客户在线进行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
为什么有些请求没有被压缩,这应该是我的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");
任何想法,谢谢。
最佳答案
仅当请求表明它可以接受压缩的响应时,响应才会压缩。这由请求中的Accept-Encoding
header 指示。因此,也许您的在线curl
没有发送该 header 。对于看到此消息的客户来说可能是相同的。您确实有一些客户报告他们没有得到答复的确认吗?
更新
啊,我明白你现在在做什么。您使用的是Varnish的最新版本吗?现在无需您自己进行所有操作。 Varnish native 处理所有这些。您需要做的就是将do_gzip
设置为on
,以获取所需的内容类型,然后Varnish会处理其余的内容,包括Accept-Encoding
header 。参见the documentation here。
因此,只需删除您所有与gzip/encoding相关的代码,除了# 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
header 吗?
关于gzip - CSS并非总是被压缩,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44803037/