【问题标题】:apache .gz gzip content handler for Linux documentation /usr/share/doc and localhost/doc/用于 Linux 文档 /usr/share/doc 和 localhost/doc/ 的 apache .gz gzip 内容处理程序
【发布时间】:2011-03-09 06:17:42
【问题描述】:

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

这将使浏览文档变得更加容易。任何在这里提供帮助的编程资源都会很好。

【问题讨论】:

    标签: apache gzip handler content-type


    【解决方案1】:

    我以前对 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 模块 deflate 安装命令。这是我的 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 看看你会得到什么结果。

    【讨论】:

    • @dolan 我可以把 部分放在我上面的 部分中吗?
    • @nelaar 据我所知,这应该可以正常工作。我更新了我的帖子以包含您的代码。如果这不起作用,请回帖
    • /etc/apache2/sites-enabled/000-default 第 41 行的语法错误:无效命令“标头”,可能拼写错误或由未包含在服务器配置中的模块定义 41 标头集内容-编码:gzip
    • 我猜 apache 模块头文件或重写丢失了。但这是我的完整列表,可帮助调试缺少的内容:别名、自动索引、cgi、dir、env、过期、标题、mime ,协商,重写,setenvif,状态(去 /etc/apache2/mods-enabled/ ,至少在 ubuntu 服务器上查看安装了什么)
    • @dolan 修复了错误但很抱歉它没有按我预期的那样工作。我的浏览器仍在尝试下载文件。 Web 服务器未将其作为纯文本发送。
    【解决方案2】:
    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
    

    【讨论】:

    • 我取消了我的注释。它仍然给我带来问题
    • 对我来说,在您注释掉以AddType 开头的行后,它就可以工作了。
    猜你喜欢
    • 2011-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多