mod_deflate

mod_deflate mod_deflate 实时压缩内容

deflate (操作)

等待响应头;如果响应可以被压缩,deflate 会添加一个过滤器来压缩它

deflate options;
选项
一个包含以下条目的键值表
编码方式
支持的方法,取决于编译时包含的模块 (默认值: "deflate,gzip,bzip2")
块大小
块大小是每次压缩的千字节数,它允许 Web 服务器在压缩间隙执行其他工作(网络 I/O) (默认值: 4096)
输出缓冲区
输出缓冲区是每个连接的压缩输出缓冲区,它有助于减小响应大小(减少编码块数)。如果设置为零,将使用共享缓冲区。(默认值: 4096)
压缩级别
0-9:较低的数字意味着更快的压缩速度,但会产生更大的文件/输出;较高的数字可能需要更长的压缩时间,但会产生更小的文件/输出(取决于文件是否容易被压缩),此选项适用于所有选定的编码变体 (默认值: 1)

示例

deflate [ "encodings" => "deflate,gzip,bzip2", "blocksize" => 4096, "output-buffer" => 4096, "compression-level" => 1 ];

示例

deflate [ "compression-level" => 6 ];

deflate.debug (选项)

启用调试输出

deflate.debug value;
默认值: false

注意事项

重要: 由于 deflate; 会等待响应头,您必须在其之前处理请求(参阅下文如何检查请求是否已处理)。如果请求未被处理,您将收到“500 - 内部错误”以及 error.log 中的一条消息。

不压缩

  • 响应状态: 100, 101, 204, 205, 206, 304
  • 已压缩内容
  • 如果发送了多个 etag 响应头
  • 如果未找到通用编码

支持的编码方式

  • gzip, deflate (需要 zlib)
  • bzip2 (需要 bzip2)

deflate 还会

  • 修改 etag 响应头(如果存在)
  • 添加“Vary: Accept-Encoding”响应头
  • 重置 Content-Length 头

简单配置

setup {
	module_load "mod_deflate";
}

# define our own "action"
do_deflate = {
	# make sure static files get handled before we try deflate
	static;
	# we can only wait for response headers if we already have a request handler! (static only handles GET/HEAD requests)
	if request.is_handled {
		# limit content-types we want to compress -> see mimetypes
		if response.header["Content-Type"] =~ "^(.*/javascript|text/.*)(;|$)" {
			deflate;
		}
	}
};

# now add do_deflate; in places where you want deflate. for example at the end of your config:

do_deflate;

扩展配置 (利用 mod_cache_disk_etag)

setup {
	module_load ("mod_deflate", "mod_cache_disk_etag");
}

# define our own "action"
do_deflate = {
	# make sure static files get handled before we try deflate
	static;
	# we can only wait for response headers if we already have a request handler! (static only handles GET/HEAD requests)
	if request.is_handled {
		# limit content-types we want to compress -> see mimetypes
		if response.header["Content-Type"] =~ "^(.*/javascript|text/.*)(;|$)" {
			deflate;
			# the following block needs mod_cache_disk_etag (and is optional)
			# only cache compressed result of static files
			if physical.is_file {
				cache.disk.etag "/var/cache/lighttpd/cache_etag";
			}
		}
	}
};

# now add do_deflate; in places where you want deflate. for example at the end of your config:

do_deflate;