webserver - 服务器端 GZipping 如何工作?

标签 webserver gzip content-encoding

您可能知道 HTML 相关文件格式是使用服务器端 GZip 压缩(通过 Apache 服务器上的 mod_gzip)进行压缩的,并由兼容的浏览器解压缩。 (“内容编码”)

这仅适用于 HTML/XML 文件吗?假设我的 PHP/Perl 文件生成一些简单的逗号分隔数据,并将其发送到浏览器,默认情况下会对其进行编码吗?

像Silverlight或Flash这样的平台呢,当它们下载这些数据时,浏览器/运行时会自动压缩/解压缩吗?有什么办法可以测试一下吗?

最佳答案

Does this only work for HTML/XML files?

不:它经常用于 CSS 和 JS 文件,例如,因为它们是网站构成的最大的东西之一(图像除外),由于 JS 框架和完整的 JS 应用程序,它代表了收获巨大!

实际上,任何基于文本的格式都可以很好地压缩(相反,例如图像则不能,因为它们通常已经被压缩);有时,从 Ajax 请求返回的 JSON 数据也会被压缩——毕竟它是文本数据;-)

Lets say my PHP/Perl file generates some simple comma delimited data, and sends that to the browser, will it be encoded by default?

这是一个配置问题:如果您将服务器配置为压缩此类内容,那么它可能会被压缩:-)
(如果浏览器表示它接受 gzip 编码的数据)


以下是我在博客上使用的 Apache 2 配置示例(使用 mod_deflate):

<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript application/x-javascript application/xml
</IfModule>

在这里,我想要压缩 html/xml/css/JS。

这是同样的事情,加上/减去我在 Apache 1 (mod_gzip) 下使用过的一些配置选项:

<IfModule mod_gzip.c>
    mod_gzip_on                   Yes
    mod_gzip_can_negotiate        Yes

    mod_gzip_minimum_file_size    256
    mod_gzip_maximum_file_size    500000

    mod_gzip_dechunk              Yes

    mod_gzip_item_include         file       \.css$
    mod_gzip_item_include         file       \.html$
    mod_gzip_item_include         file       \.txt$
    mod_gzip_item_include         file       \.js$
    mod_gzip_item_include         mime       text/html

    mod_gzip_item_exclude         mime       ^image/
</IfModule>

这里需要注意的是,我不希望压缩的文件太小(增益不是很重要)或太大(会消耗太多的 CPU 来压缩);我希望压缩 css/html/txt/js 文件,但不压缩图像。


如果您希望以相同的方式压缩逗号分隔的数据,则必须将其内容类型或其扩展名添加到网络服务器的配置中,以为其激活 gzip 压缩。

Is there any way to test this?

对于直接返回到浏览器的任何内容,Firefox 的扩展 FirebugLiveHTTPHeaders是必备的。

对于不通过浏览器标准通信方式的内容,可能会更困难;最后,您可能不得不使用类似 Wireshark 的东西“嗅探”真正通过管道的东西......祝你好运!

What about platforms like Silverlight or Flash, when they download such data will it be compressed/decompressed by the browser/runtime automatically?

为了回答您有关 Silverlight 和 Flash 的问题,如果它们发送 Accept header 表明它们支持压缩内容,Apache 将使用 mod_deflate 或 mod_gzip。如果他们不支持压缩,他们将不会发送 header 。它会“正常工作”。 –Nate

关于webserver - 服务器端 GZipping 如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1251908/

相关文章:

asp.net-core - 替代 HttpResponse.ContentEncoding ASP.NET 5

html - 你能通过html链接一个css文件吗?

asp.net - 'runAllManagedModulesForAllRequests' 设置为 'true' 时的静态压缩

linux - 下载网页的脚本

python - 在 Windows 上使用 Python 连接 gzipped 文件

gzip - 如何在不解压缩的情况下检查 Unix .tar.gz 文件是否是有效文件?

http - 为什么传输编码 :Chunked sent instead of Content-Encoding:gzip for some of clients?

apache - 压缩还是不压缩?

javascript - 本地机器上的 Web 服务器,有什么问题吗?

java - 如何在服务器上托管jsp网站(页面)?