apache .gz用于Linux文档的gzip内容处理程序/ usr / share / doc和localhost / doc /

时间:2011-03-09 06:17:42

标签: apache gzip handler content-type

我如何为apache .gz gzip内容创建一个简单的内容处理程序。我希望它解压缩说http://localhost/doc/FAQ/Linux-FAQ.gz并将其作为纯文本发送到浏览器。在/ usr / share / doc和localhost / doc /中有很多关于Linux的文档。我不想使用zless,zcat或vim来阅读内容。我使用apache浏览本地计算机上的文档,并让我的Web浏览器将其恢复为标准文本,这样它就不会要求我每次都下载* .gz文件。

Alias /doc/ "/usr/share/doc/"
Alias local.doc "/usr/share/doc/"
<Directory "/usr/share/doc/">
    Options Indexes MultiViews FollowSymLinks
    AllowOverride None
    Order deny,allow
    Deny from all
    Allow from 127.0.0.0/255.0.0.0 ::1/128
</Directory>

但现在我希望/ usr / share / doc /下的所有.gz文件都被视为纯文本。我想我可以用cgi-bin中的python脚本非常简单地做到这一点。我正在为这些文件寻找一个很好的内容处理程序。就像处理php文件一样.gz应该是解压缩并发送到浏览器。

<IfModule mod_php5.c>
  AddType application/x-httpd-php .php .phtml .php3
  AddType application/x-httpd-php-source .phps
</IfModule>
LoadModule php5_module /usr/lib/apache2/modules/libphp5.so

我看到有一个mod_deflate,这将如何适用。这可以处理gzip内容吗?

这将使浏览文档变得更加容易。任何有助于此的编程资源都会很好。

2 个答案:

答案 0 :(得分:7)

之前我曾经使用过类似的东西用于js / css文件(我修改了下面的内容以满足你的需求)。将其添加到您的虚拟主机条目:

Alias /doc/ "/usr/share/doc/"
Alias local.doc "/usr/share/doc/"
<Directory /usr/share/doc>
    Options Indexes MultiViews FollowSymLinks
    AllowOverride None
    Order deny,allow
    Deny from all
    Allow from 127.0.0.0/255.0.0.0 ::1/128

    AddEncoding gzip gz
    <FilesMatch "\.gz$">
      ForceType text/plain
      Header set Content-Encoding: gzip
    </FilesMatch>
</Directory>

以上更新以匹配您的代码

在ubuntu中确保启用了Headers模块

$ sudo a2enmod headers  
$ sudo a2enmod deflate
$ sudo apache2ctl restart

Update2:意识到“AddEncoding gzip gz”丢失了..否则,文件一直试图下载。

Update3:添加了apache module deflate install命令。这是我的deflate.conf:

<IfModule mod_deflate.c>
      # these are known to be safe with MSIE 6
      AddOutputFilterByType DEFLATE text/html text/plain text/xml

      # everything else may cause problems with MSIE 6
      AddOutputFilterByType DEFLATE text/css
      AddOutputFilterByType DEFLATE application/x-javascript application/javascript application/ecmascript
      AddOutputFilterByType DEFLATE application/rss+xml
</IfModule>

您可以先尝试使用其他类型的文件(例如css文件)。例如:

cd /usr/share/doc
cat ".styles { width: 50px; }" > test.css
gzip -c test.css > test.css.gz

将此添加到您的虚拟主机:

    <FilesMatch "\.css\.gz$">
        ForceType text/css
        Header set Content-Encoding: gzip
    </FilesMatch>

测试http://127.0.0.1/doc/test.csshttp://127.0.0.1/doc/test.css.gz,看看你得到的结果。

答案 1 :(得分:0)

cat /etc/apache2/mods-enabled/mime.conf | head -n 30
<IfModule mod_mime.c>

#
# TypesConfig points to the file containing the list of mappings from
# filename extension to MIME-type.
#
TypesConfig /etc/mime.types

#
# AddType allows you to add to or override the MIME configuration
# file mime.types for specific file types.
#
#AddType application/x-gzip .tgz
#
# AddEncoding allows you to have certain browsers uncompress
# information on the fly. Note: Not all browsers support this.
# Despite the name similarity, the following Add* directives have
# nothing to do with the FancyIndexing customization directives above.
#
AddEncoding x-compress .Z
AddEncoding x-gzip .gz .tgz
AddEncoding x-bzip2 .bz2
#
# If the AddEncoding directives above are commented-out, then you
# probably should define those extensions to indicate media types:
#
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
AddType application/x-bzip2 .bz2