如何在 Apache 2.x 中使用 mod_deflate 预压缩文件?

发布于 2024-07-04 03:51:13 字数 240 浏览 8 评论 0原文

我通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

唔猫 2024-07-11 03:51:13

我有一个从源代码构建的 Apache 2,我发现我必须在 httpd.conf 文件中修改以下内容:

将 MultiViews 添加到选项:

选项索引FollowSymLinks多视图 
  

取消注释 AddEncoding:

AddEncoding x-compress .Z 
  添加编码 x-gzip .gz .tgz 
  

评论添加类型:

#AddType application/x-compress .Z 
  #AddType 应用程序/x-gzip .gz .tgz 
  

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:

Options Indexes FollowSymLinks MultiViews

Uncomment AddEncoding:

AddEncoding x-compress .Z
AddEncoding x-gzip .gz .tgz

Comment AddType:

#AddType application/x-compress .Z
#AddType application/x-gzip .gz .tgz
平生欢 2024-07-11 03:51:13

我担心 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.

胡大本事 2024-07-11 03:51:13

可以使用 mod_negotiation 提供预压缩文件,尽管它有点挑剔。 主要困难是 仅请求不存在的文件经协商。 因此,如果 foo.jsfoo.js.gz 都存在,则 /foo.js 的响应将始终未压缩(尽管 /foo.js 的响应将始终未压缩) code>/foo 会正常工作)。

我找到的最简单的解决方案(来自 François Marier) 是用双文件扩展名重命名未压缩的文件,因此 foo.js 部署为 foo.js.js,因此请求 /foo.jsfoo.js.js (无编码)和 foo.js.gz (gzip 编码)之间进行协商。

我将该技巧与以下配置结合起来:

Options +MultiViews
RemoveType .gz
AddEncoding gzip .gz

# Send .tar.gz without Content-Encoding: gzip
<FilesMatch ".+\.tar\.gz$">
    RemoveEncoding .gz
    # Note:  Can use application/x-gzip for backwards-compatibility
    AddType application/gzip .gz
</FilesMatch>

写了一篇文章,详细讨论了此配置的原因和一些替代方案。

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 if foo.js and foo.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 as foo.js.js so requests for /foo.js negotiate between foo.js.js (no encoding) and foo.js.gz (gzip encoding).

I combine that trick with the following configuration:

Options +MultiViews
RemoveType .gz
AddEncoding gzip .gz

# Send .tar.gz without Content-Encoding: gzip
<FilesMatch ".+\.tar\.gz$">
    RemoveEncoding .gz
    # Note:  Can use application/x-gzip for backwards-compatibility
    AddType application/gzip .gz
</FilesMatch>

I wrote a post which discusses the reasoning for this configuration and some alternatives in detail.

情徒 2024-07-11 03:51:13

您可以使用 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 with mod_deflate.

演出会有结束 2024-07-11 03:51:13

我有很多大的 .json 文件。 大多数读者都处于这种情况。 预览答案没有谈论返回的“Content-type”。

我希望以下请求透明地返回一个带有“Content-Type: application/json”的预压缩文件,请使用 Multiview 和 ForceType

http://www.domain.com/(...)/bigfile.json
-> Content-Encoding:gzip, Content-Type: Content-Encoding:gzip

1) 文件必须重命名:“file.ext.ext”

2) Multiview 与 ForceType 配合使用效果

很好文件系统:

// Note there is no bigfile.json
(...)/bigfile.json.gz
(...)/bigfile.json.json

在你的 apache 配置中:

<Directory (...)>
    AddEncoding gzip .gz
    Options +Multiviews
    <Files *.json.gz>
        ForceType application/json
    </Files>
</Directory>

简短:)

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

http://www.domain.com/(...)/bigfile.json
-> Content-Encoding:gzip, Content-Type: Content-Encoding:gzip

1) files must be rename: "file.ext.ext"

2) Multiview works great with ForceType

In the file system:

// Note there is no bigfile.json
(...)/bigfile.json.gz
(...)/bigfile.json.json

In your apache config:

<Directory (...)>
    AddEncoding gzip .gz
    Options +Multiviews
    <Files *.json.gz>
        ForceType application/json
    </Files>
</Directory>

Short and simple :)

少跟Wǒ拽 2024-07-11 03:51:13

mod_gzip 也可以动态压缩内容。 您可以通过实际登录服务器并从 shell 执行此操作来预压缩文件。

cd /var/www/.../data/
for file in *; do
    gzip -c $file > $file.gz;
done;

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.

cd /var/www/.../data/
for file in *; do
    gzip -c $file > $file.gz;
done;
千寻… 2024-07-11 03:51:13

用我在配置中缺少的非常简单的一行来回答我自己的问题:

Options FollowSymLinks MultiViews

我缺少 MultiViews 选项。 它存在于 Ubuntu 默认 Web 服务器配置中,所以不要像我一样将其删除。

我还编写了一个快速 Rake 任务来压缩所有文件。

namespace :static do
    desc "Gzip compress the static content so Apache doesn't need to do it on-the-fly."
    task :compress do
        puts "Gzipping js, html and css files."
        Dir.glob("#{RAILS_ROOT}/public/**/*.{js,html,css}") do |file|
            system "gzip -c -9 #{file} > #{file}.gz"
        end
    end
end

To answer my own question with the really simple line I was missing in my confiuration:

Options FollowSymLinks MultiViews

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.

namespace :static do
    desc "Gzip compress the static content so Apache doesn't need to do it on-the-fly."
    task :compress do
        puts "Gzipping js, html and css files."
        Dir.glob("#{RAILS_ROOT}/public/**/*.{js,html,css}") do |file|
            system "gzip -c -9 #{file} > #{file}.gz"
        end
    end
end
沉溺在你眼里的海 2024-07-11 03:51:13

无论如何,这个功能在 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 the Options directive and you need to specify your encoding types with the AddEncoding directive.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文