Apache根据Accept-Encoding标头提供压缩/未压缩文件

时间:2015-01-28 17:31:34

标签: apache gzip compression deflate http-accept-encoding

以下问题与this一个问题高度相关。

Apache版本是2.2,所以没有"如果"声明提供。 服务器仅包含gzip压缩文件,以节省空间。用户可以请求gzip压缩文件或仅解压缩内容,具体取决于Accept-Encoding。

这是httpd.conf的伪配置:

<Directory /data/compressed>
    RewriteEngine on

    # Check file exists
    RewriteCond %{REQUEST_FILENAME}gz -s

    # Append a .gz to the requested filename
    RewriteRule ^(.+)\.txt$ $1\.txt\.gz [QSA]

    # If request contains "Accept-Encoding: gzip":
         # Set the appropriate Content-Type and Content-Encoding
         Header set Content-Type "application/gzip"
         Header set Content-Encoding "gzip"
    # Else:
         # Decompress the .gz
         FilterDeclare gzip CONTENT_SET
         FilterProvider gzip INFLATE req=Accept-Encoding !$gzip
         FilterChain gzip

         # Set the appropriate Content-Type and Content-Encoding
         Header set Content-Type "text/plain"
         Header unset Content-Encoding

         # Serve the decompressed (.txt) file, without ending ".gz"
         # ???

</Directory>

因此,如果客户端可以处理压缩内容,服务器将只检索.gz文件(注意:客户端专门请求&#34; file.txt&#34; not&#34; file.txt.gz&#34;并服务它。如果客户端无法处理压缩内容,服务器将找到.gz文件,解压缩,相应地更改标头,然后提供文件。

我问你的帮助,因为找不到使用Apache规则评估if块的方法。此外,我想知道在提供之前如何从未压缩文件中删除.gz。

谢谢。

0 个答案:

没有答案
相关问题