代码之家  ›  专栏  ›  技术社区  ›  Otto

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

  •  34
  • Otto  · 技术社区  · 17 年前

    我通过apache提供所有内容 Content-Encoding: zip 但这会在飞行中压缩。我的大部分内容都是磁盘上的静态文件。我想提前对文件进行gzip压缩,而不是每次请求时都进行压缩。

    我相信, mod_gzip 在Apache 1.x中是自动执行的,但旁边只有.gz文件 mod_deflate .

    8 回复  |  直到 17 年前
        1
  •  13
  •   Aristotle Pagaltzis    17 年前

    无论如何,这个功能在mod_gzip中都放错了位置。在Apache 2.x中, you do that with content negotiation 。具体来说,您需要启用 MultiViews Options directive 您需要使用指定编码类型 AddEncoding directive .

        2
  •  8
  •   Otto    17 年前

    用我在确认中遗漏的一句非常简单的话来回答我自己的问题:

    Options FollowSymLinks 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
    
        3
  •  5
  •   Arno Schäfer    14 年前

    恐怕MultiViews不会按预期工作:文档说MultiViews工作“如果服务器收到对/some/dir/foo的请求,如果/some/dir启用了MultiViews,并且/some/di/foo不存在…”,换句话说:如果你在同一目录中有一个文件foo.js和foo.js.gz,即使浏览器传输了AcceptEncoding gzip标头,仅仅激活MultiViews也不会导致.gz文件被发送(你可以通过暂时禁用mod_deflate并用HTTPFox等监视响应来验证这种行为)。

    我不确定MultiViews是否有办法解决这个问题(也许你可以重命名原始文件,然后添加一个特殊的AddEncoding指令),但我相信你可以构造一个mod_rewrite规则来处理这个问题。

        4
  •  5
  •   Kevinoid    9 年前

    可以使用以下方式提供预压缩文件 mod_negotiation 虽然有点挑剔。主要困难在于 only requests for files which do not exist are negotiated 那么,如果 foo.js foo.js.gz 两者都存在,对 /foo.js 将始终未压缩(尽管响应为 /foo 将正常工作)。

    我找到的最简单的解决方案( from François Marier )是用双文件扩展名重命名未压缩的文件,因此 foo.js 部署为 foo.js.js 因此,请求 /foo.js 协商双方 foo.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>
    

    一、 wrote a post 详细讨论了这种配置的推理和一些替代方案。

        5
  •  4
  •   brianegge    17 年前

    我有一个从源代码构建的Apache 2,我发现我必须在我的 httpd.conf文件 文件:

    将多视图添加到选项:

    Options Indexes FollowSymLinks MultiViews
    

    未注释添加编码:

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

    评论添加类型:

    #AddType application/x-compress .Z
    #AddType application/x-gzip .gz .tgz
    
        6
  •  1
  •   Aeon    17 年前

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

    cd /var/www/.../data/
    for file in *; do
        gzip -c $file > $file.gz;
    done;
    
        7
  •  0
  •   Brian Matthews    17 年前

    您可以使用 mod_cache 代理内存或磁盘上的本地内容。我不知道这是否会像预期的那样奏效 mod_deflate .

        8
  •  0
  •   Vivien    11 年前

    我有很多很大的.json文件。大多数读者都处于这种情况。预览答案没有提及返回的“内容类型”。

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

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

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

    2) 多视图与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>
    

    简洁明了:)

    推荐文章