我在 NGinx 服务器上有一个 Web 应用程序。我在conf文件中设置了
gzip on
,现在我想看看它是否有效。 YSlow 说不是,但进行测试的 6 个网站中有 5 个表示是。我怎样才能得到明确的答案以及为什么结果会有所不同?
curl
:
$ curl http://example.com/ --silent --write-out "%{size_download}\n" --output /dev/null
31032
$ curl http://example.com/ --silent -H "Accept-Encoding: gzip,deflate" --write-out "%{size_download}\n" --output /dev/null
2553
在第二种情况下,客户端告诉服务器它支持内容编码,您可以看到响应确实更短、压缩。
更新
Chrome 改变了它的报告方式(如果感兴趣,请参阅原始答案)。您可以使用开发人员工具 (F12) 来判断。转到“网络”选项卡,选择要检查的文件,然后查看右侧的“标头”选项卡。如果您经过 gzip 压缩,那么您将在内容编码中看到它。
在此示例中,slider.jpg 确实被 gzip 压缩。
将其与您所在的页面进行比较并查看 png 文件,您将看不到这样的指定。
需要明确的是,这并不是因为一个是 jpg,一个是 png。这是因为一个是 gzip 压缩的,而另一个不是。
之前的回答
在 Chrome 中,如果您拉起开发者工具并转到“网络”选项卡,那么如果没有压缩,它将显示以下内容:
如果存在压缩,则如下:
换句话说,顶部和底部的数字相同,意味着没有压缩。
请参阅响应标头。在 FireFox 中,您可以使用 Firebug 检查。
Content-Encoding gzip
如果服务器支持 gzip 内容,则应显示此内容。
您可以快速使用网络服务,例如:http://www.whatsmyip.org/http-compression-test/
Google Chrome 开发者工具中的“审核”工具也派上用场。
我根据zoul的回答写了这个脚本:
#!/bin/bash
URL=$1
PLAIN="$(curl $URL --silent --write-out "%{size_download}\n" --output /dev/null)"
GZIPPED="$(curl $URL --silent -H "Accept-Encoding: gzip,deflate" --write-out "%{size_download}\n" --output /dev/null)"
if test $PLAIN -gt $GZIPPED
then echo "supported"
else echo "unsupported"
fi
示例:
$ ./script.sh https://example.com/
这是我的一句台词:
curl https://google.com --silent -L -H "Accept-Encoding: gzip,deflate" --output testFile && file testFile
file
命令行工具会告诉您它是什么类型的文件。你应该看到这个:
testFile: gzip compressed data, max compression, original size modulo 2^32 13926
由于这是谷歌中的第一个搜索结果,我将编辑其中一个答案以包含 Brotli 压缩,它的短名称是
br
,您可以将它与 curl
一起使用或通过 F12
观看它浏览器。
for
curl
编辑自@zoul answer:
$ curl http://example.com/ --silent --write-out "%{size_download}\n" --output /dev/null
31032
$ curl http://example.com/ --silent -H "Accept-Encoding: gzip,deflate,br" --write-out "%{size_download}\n" --output /dev/null
2553