如何判断我的服务器是否正在提供 GZipped 内容?

问题描述 投票:0回答:8

我在 NGinx 服务器上有一个 Web 应用程序。我在conf文件中设置了

gzip on
,现在我想看看它是否有效。 YSlow 说不是,但进行测试的 6 个网站中有 5 个表示是。我怎样才能得到明确的答案以及为什么结果会有所不同?

nginx compression gzip
8个回答
283
投票

毫不奇怪,看起来一个可能的答案是

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

在第二种情况下,客户端告诉服务器它支持内容编码,您可以看到响应确实更短、压缩。


162
投票

更新

Chrome 改变了它的报告方式(如果感兴趣,请参阅原始答案)。您可以使用开发人员工具 (F12) 来判断。转到“网络”选项卡,选择要检查的文件,然后查看右侧的“标头”选项卡。如果您经过 gzip 压缩,那么您将在内容编码中看到它。

在此示例中,slider.jpg 确实被 gzip 压缩。

将其与您所在的页面进行比较并查看 png 文件,您将看不到这样的指定。

需要明确的是,这并不是因为一个是 jpg,一个是 png。这是因为一个是 gzip 压缩的,而另一个不是。


之前的回答

在 Chrome 中,如果您拉起开发者工具并转到“网络”选项卡,那么如果没有压缩,它将显示以下内容:

enter image description here

如果存在压缩,则如下:

enter image description here

换句话说,顶部和底部的数字相同,意味着没有压缩。


36
投票

请参阅响应标头。在 FireFox 中,您可以使用 Firebug 检查。

Content-Encoding    gzip

如果服务器支持 gzip 内容,则应显示此内容。


20
投票

在新版本的chrome中,开发者工具>网络,您可以右键单击列名称,然后选择内容编码选项并添加该列(图像中的黑框)。

如果您想查看 gzip 内容的大小,例如 @Outfast Source - 您可以单击“查看”旁边的图标(在图像中显示为绿色框)。

这样您就可以看到哪些内容启用了 gzip。


11
投票

您可以快速使用网络服务,例如:http://www.whatsmyip.org/http-compression-test/

Google Chrome 开发者工具中的“审核”工具也派上用场。


9
投票

我根据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/

2
投票

这是我的一句台词:

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

1
投票

由于这是谷歌中的第一个搜索结果,我将编辑其中一个答案以包含 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
© www.soinside.com 2019 - 2024. All rights reserved.