我在一个NGinx服务器上有一个web应用程序。我在配置文件中设置了gzip on
,现在我正在尝试查看它是否起作用。YSlow说它没有起作用,但是进行测试的6个网站中有5个说它已经启用了。我如何得到明确的答案,并且为什么结果会有差异?
我在一个NGinx服务器上有一个web应用程序。我在配置文件中设置了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)进行检查。转到网络选项卡,选择要检查的文件,然后查看右侧的标头选项卡。如果已经压缩,则在内容编码中将其显示出来。
在此示例中,slider.jpg确实被压缩了。
将其与您正在访问的此页面中的png文件进行比较,您将看不到这样的指定。
只是为了明确,这并不是因为一个是jpg,而另一个是png。这是因为一个被压缩了,而另一个没有。
先前的答案
在Chrome中,如果您打开开发者工具并转到“网络”选项卡,则会显示以下内容,如果没有压缩,则会显示以下内容:
如果有压缩,则会显示以下内容:
换句话说,相同的数字,上下都是一样的,表示没有压缩。
gzip
。可以在响应头中查看。在 FireFox 中,您可以使用 Firebug 进行检查。
Content-Encoding gzip
如果服务器支持gzip内容,则应该显示此内容。
你可以快速使用像这样的Web服务: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
进行监视。
对于从@zoul answer编辑的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,br" --write-out "%{size_download}\n" --output /dev/null
2553
curl
时也加上-L
标志以便跟随所有重定向,例如从非www网址跳转到www
,否则就可能导致size_download
的结果不准确。 - vladkras