如何在 Apache 2.x 中使用 mod_deflate 预压缩文件?
我通过 apache 使用 Content-Encoding: zip
提供所有内容,但会动态压缩。 我的大部分内容都是磁盘上的静态文件。 我想预先对文件进行 gzip 压缩,而不是每次请求时都对其进行压缩。
我相信这是 mod_gzip
在 Apache 1.x 中自动完成的事情,但只是让文件旁边带有 .gz。 mod_deflate
不再是这种情况。
I am serving all content through apache with Content-Encoding: zip
but that compresses on the fly. A good amount of my content is static files on the disk. I want to gzip the files beforehand rather than compressing them every time they are requested.
This is something that, I believe, mod_gzip
did in Apache 1.x automatically, but just having the file with .gz next to it. That's no longer the case with mod_deflate
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我有一个从源代码构建的 Apache 2,我发现我必须在 httpd.conf 文件中修改以下内容:
将 MultiViews 添加到选项:
取消注释 AddEncoding:
评论添加类型:
I have an Apache 2 built from source, and I found I had to modify the following in my httpd.conf file:
Add MultiViews to Options:
Uncomment AddEncoding:
Comment AddType:
我担心 MultiViews 不会按预期工作:文档说 Multiviews 工作“如果服务器收到对 /some/dir/foo 的请求,如果 /some/dir 启用了 MultiViews,并且 /some/dir/foo 不存在。 ..”,换句话说:如果您在同一目录下有文件 foo.js 和 foo.js.gz,则仅激活 MultiViews 不会导致 .gz 文件被发送,即使 AcceptEncoding gzip 标头是由浏览器(您可以通过暂时禁用 mod_deflate 并使用 HTTPFox 等监控响应来验证此行为)。
我不确定是否有办法通过 MultiViews 解决这个问题(也许您可以重命名原始文件,然后添加一个特殊的 AddEncoding 指令),但我相信您可以构造一个 mod_rewrite 规则来处理这个问题。
I am afraid MultiViews will not work as expected: the doc says Multiviews works "if the server receives a request for /some/dir/foo, if /some/dir has MultiViews enabled, and /some/dir/foo does not exist...", in other words: if you have a file foo.js and foo.js.gz in the same directory, just activating MultiViews will not cause the .gz file to be sent even if the AcceptEncoding gzip header is transmitted by the browser (you can verify this behavior by temporarily disabling mod_deflate and monitoring the response with e.g. HTTPFox).
I am not sure if there is a way around this with MultiViews (maybe you can rename the original file and then add a special AddEncoding directive), but I believe you can construct a mod_rewrite rule to handle this.
可以使用 mod_negotiation 提供预压缩文件,尽管它有点挑剔。 主要困难是 仅请求不存在的文件经协商。 因此,如果
foo.js
和foo.js.gz
都存在,则/foo.js
的响应将始终未压缩(尽管/foo.js
的响应将始终未压缩) code>/foo 会正常工作)。我找到的最简单的解决方案(来自 François Marier) 是用双文件扩展名重命名未压缩的文件,因此
foo.js
部署为foo.js.js
,因此请求/foo.js
在foo.js.js
(无编码)和foo.js.gz
(gzip 编码)之间进行协商。我将该技巧与以下配置结合起来:
我 写了一篇文章,详细讨论了此配置的原因和一些替代方案。
It is possible to serve pre-compressed files using
mod_negotiation
although it is a bit finicky. The primary difficulty is that only requests for files which do not exist are negotiated. So iffoo.js
andfoo.js.gz
both exist, responses for/foo.js
will always be uncompressed (although responses for/foo
would work correctly).The easiest solution I've found (from François Marier) is to rename uncompressed files with a double file extension, so
foo.js
is deployed asfoo.js.js
so requests for/foo.js
negotiate betweenfoo.js.js
(no encoding) andfoo.js.gz
(gzip encoding).I combine that trick with the following configuration:
I wrote a post which discusses the reasoning for this configuration and some alternatives in detail.
您可以使用
mod_cache
来代理本地内存或磁盘上的内容。 我不知道这是否会按mod_deflate
的预期工作。You can use
mod_cache
to proxy local content in memory or on disk. I don't know if this will work as expected withmod_deflate
.我有很多大的 .json 文件。 大多数读者都处于这种情况。 预览答案没有谈论返回的“Content-type”。
我希望以下请求透明地返回一个带有“Content-Type: application/json”的预压缩文件,请使用 Multiview 和 ForceType
1) 文件必须重命名:“file.ext.ext”
2) Multiview 与 ForceType 配合使用效果
很好文件系统:
在你的 apache 配置中:
简短:)
I have a lot of big .json files. Most readers are in this situation. The preview answers didn't talk about the returned "Content-type".
I you want the following request return a pre-compressed file with "Content-Type: application/json" transparently, use Multiview with ForceType
1) files must be rename: "file.ext.ext"
2) Multiview works great with ForceType
In the file system:
In your apache config:
Short and simple :)
mod_gzip 也可以动态压缩内容。 您可以通过实际登录服务器并从 shell 执行此操作来预压缩文件。
mod_gzip compressed content on the fly as well. You can pre-compress the files by actually logging into your server, and doing it from shell.
用我在配置中缺少的非常简单的一行来回答我自己的问题:
我缺少 MultiViews 选项。 它存在于 Ubuntu 默认 Web 服务器配置中,所以不要像我一样将其删除。
我还编写了一个快速 Rake 任务来压缩所有文件。
To answer my own question with the really simple line I was missing in my confiuration:
I was missing the MultiViews option. It's there in the Ubuntu default web server configuration, so don't be like me and drop it off.
Also I wrote a quick Rake task to compress all the files.
无论如何,这个功能在 mod_gzip 中被放错了位置。 在 Apache 2.x 中,您可以通过内容协商来实现这一点。 具体来说,您需要使用 < 启用
MultiViews
code>Options 指令 并且您需要使用AddEncoding
指令。This functionality was misplaced in mod_gzip anyway. In Apache 2.x, you do that with content negotiation. Specifically, you need to enable
MultiViews
with theOptions
directive and you need to specify your encoding types with theAddEncoding
directive.