项目

通用

个人资料

操作

输出压缩

模块: mod_compress

描述

已弃用 自lighttpd 1.4.56起,mod_compress 已被 mod_deflate 取代。

输出压缩可减少网络负载,并能提高Web服务器的整体
吞吐量。所有主要的HTTP客户端都通过在Accept-Encoding头中
声明来支持压缩。这用于协商
最合适的压缩方法。我们支持deflate、gzip和bzip2。

deflate (RFC1950, RFC1951) 和 gzip (RFC1952) 依赖于zlib,而bzip2
依赖于libbzip2。bzip2仅受lynx和一些其他控制台
文本浏览器支持。

目前我们仅支持对静态文件的压缩。

缓存

mod_compress 可以将压缩文件存储在磁盘上,以优化
第二次请求时的压缩过程。一旦设置了 compress.cache-dir,文件就会被
压缩。

(如果缓存目录不存在,您需要自行创建。Web
服务器不会为您创建。该目录还需要正确的
权限。对于Debian/Ubuntu,用户和组ID都应为www-data。)

缓存文件的名称由文件名、压缩方法
以及与该文件关联的 etag 组成。

清理缓存由用户自行处理。一个删除10天前文件的cron作业
可以完成此操作: :

  find /var/www/cache -type f -mtime +10 | xargs -r rm

限制

该模块将文件压缩限制在小于128 MB且
大于128字节的文件。

设置下限是因为小文件经过压缩后,由于
压缩头的原因,往往会变大;设置上限是为了在内存和
CPU时间方面合理工作。实际上,如果您的服务器配置较低,禁用 mod_compress 可能会获得更好的性能。

选项

compress.allowed-encodings
覆盖默认允许的编码集

e.g.: ::
    compress.allowed-encodings = ("bzip2", "gzip", "deflate")

compress.cache-dir
缓存压缩内容的目录名称

e.g.: ::
    compress.cache-dir = "/var/www/cache/" 

    # even better with virt-hosting
    $HTTP["host"] == "docs.example.org" {
      compress.cache-dir = "/var/www/cache/docs.example.org/" 
    }

Default: not set, compress the file for every request

compress.filetype
可能被压缩的mimetypes(自1.4.24起,“简单”文件类型也匹配“扩展”mimetypes,例如“text/plain”也将压缩“text/plain; charset=utf-8”)

e.g.: ::
  
    compress.filetype           = ("text/plain", "text/html")

Keep in mind that compressed JavaScript and CSS files are broken in some
browsers. Not setting any filetypes will result in no files being compressed.
NOTE: You have to specify the full mime-type! If you also define a charset,
for example, you have to use "text/plain; charset=utf-8" instead of just "text/plain".
Default: not set

compress.max-filesize
原始文件要压缩的最大大小,单位为kBytes。

This is meant to protect the server against DoSing as compressing large
(let's say 1Gbyte) takes a lot of time and would delay the whole operation
of the server.
There is a hard upper limit of 128Mbyte.
Default: unlimited (== hard-limit of 128MByte)

compress.max-loadavg (自1.4.43起)
绕过压缩前的最大系统负载平均值,例如“3.50”

显示压缩文件

如果您启用了mod_compress,并且希望强制客户端解压缩并显示
压缩的文本文件,请创建一个条件来强制mimetype。
示例

  $HTTP["url"] =~ "\.txt\.gz" {
    setenv.add-response-header = ( "Content-Encoding" => "gzip" )
    mimetype.assign = (".txt.gz" => "text/plain")
  }

如果您想为解压缩和显示 diff.gz 文件添加头信息,请在您的
配置文件中添加以下部分: :

  $HTTP["url"] =~ "\.diff\.gz" {
    setenv.add-response-header = ( "Content-Encoding" => "gzip" )
    mimetype.assign = ()
  }

压缩动态内容

PHP

要使用PHP压缩动态内容,请启用:

  zlib.output_compression = On

在php.ini中,因为PHP本身提供压缩支持。请参阅PHP手册中的zlib.configuration

lighttpd 1.4 r1992 的 mod_compress 可能无法正确设置 php-fcgi 的 Content-Encoding。
解决该问题的方法是:

1. 在请求PHP文件时禁用 mod_compress:

    $HTTP["url"] !~ "\.php$" {
        compress.filetype = ("text/plain", "text/html", "text/javascript", "text/css", "text/xml")
    }

2. 启用 lighttpd 的 mod_setenv:
    server.modules  += ( "mod_setenv" )

3. 手动设置 Content-Encoding:
    $HTTP["url"] =~ "\.php$" {
      setenv.add-response-header  = ( "Content-Encoding" => "gzip")
    }

TurboGears

要使用TurboGears压缩动态内容,请启用:

  [/]
  gzip_filter.on = True
  gzip_filter.mime_types = ["application/x-javascript", "text/javascript", "text/html", "text/css", "text/plain"]

在您的TurboGears应用程序的config/app.cfg文件中。上述行
应该已经存在于文件中。您只需删除行前
的注释符号即可使其生效。

Django

要使用Django压缩动态内容,请启用GZipMiddleware:

  MIDDLEWARE_CLASSES = (
      'django.middleware.gzip.GZipMiddleware',
      ...
  )

在您的Django项目的settings.py文件中。

Catalyst

要使用Perl/Catalyst压缩动态内容,只需使用
CPAN上提供的Catalyst::Plugin::Compress::Gzip模块:

  use Catalyst qw(
      Compress::Gzip
      ...
  );

在您的主包 (MyApp.pm) 中。无需进一步配置。

gstrauss近5年前 更新 · 41个修订版本