# Apache模塊 mod_filter
| [說明](#calibre_link-11) | 根據上下文實際情況對輸出過濾器進行動態配置 |
| --- | --- |
| [狀態](#calibre_link-12) | 基本(B) |
| [模塊名](#calibre_link-13) | filter_module |
| [源文件](#calibre_link-14) | mod_filter.c |
| [兼容性](#calibre_link-58) | Version 2.1 及以后的版本中可用 |
### 概述
This module enables smart, context-sensitive configuration of output content filters. For example, apache can be configured to process different content-types through different filters, even when the content-type is not known in advance (e.g. in a proxy).
`mod_filter` works by introducing indirection into the filter chain. Instead of inserting filters in the chain, we insert a filter harness which in turn dispatches conditionally to a filter provider. Any content filter may be used as a provider to `mod_filter`; no change to existing filter modules is required (although it may be possible to simplify them).
## Smart Filtering
In the traditional filtering model, filters are inserted unconditionally using `AddOutputFilter` and family. Each filter then needs to determine whether to run, and there is little flexibility available for server admins to allow the chain to be configured dynamically.
`mod_filter` by contrast gives server administrators a great deal of flexibility in configuring the filter chain. In fact, filters can be inserted based on any Request Header, Response Header or Environment Variable. This generalises the limited flexibility offered by `AddOutputFilterByType`, and fixes it to work correctly with dynamic content, regardless of the content generator. The ability to dispatch based on Environment Variables offers the full flexibility of configuration with `mod_rewrite` to anyone who needs it.
## Filter Declarations, Providers and Chains
![[This image displays the traditional filter model]](https://box.kancloud.cn/2015-12-14_566e61b492f1e.gif) <dfn class="calibre50">Figure 1:</dfn> The traditional filter model
In the traditional model, output filters are a simple chain from the content generator (handler) to the client. This works well provided the filter chain can be correctly configured, but presents problems when the filters need to be configured dynamically based on the outcome of the handler.
![[This image shows the mod_filter model]](https://box.kancloud.cn/2015-12-14_566e614840a37.gif)
<dfn class="calibre50">Figure 2:</dfn> The `mod_filter` model
`mod_filter` works by introducing indirection into the filter chain. Instead of inserting filters in the chain, we insert a filter harness which in turn dispatches conditionally to a filter provider. Any content filter may be used as a provider to `mod_filter`; no change to existing filter modules is required (although it may be possible to simplify them). There can be multiple providers for one filter, but no more than one provider will run for any single request.
A filter chain comprises any number of instances of the filter harness, each of which may have any number of providers. A special case is that of a single provider with unconditional dispatch: this is equivalent to inserting the provider filter directly into the chain.
## Configuring the Chain
There are three stages to configuring a filter chain with `mod_filter`. For details of the directives, see below.
Declare Filters
The `FilterDeclare` directive declares a filter, assigning it a name and filter type. Required only if the filter is not the default type AP_FTYPE_RESOURCE.
Register Providers
The `FilterProvider` directive registers a provider with a filter. The filter may have been declared with `FilterDeclare`; if not, FilterProvider will implicitly declare it with the default type AP_FTYPE_RESOURCE. The provider must have been registered with `ap_register_output_filter` by some module. The remaining arguments to `FilterProvider` are a dispatch criterion and a match string. The former may be an HTTP request or response header, an environment variable, or the Handler used by this request. The latter is matched to it for each request, to determine whether this provider will be used to implement the filter for this request.
Configure the Chain
The above directives build components of a smart filter chain, but do not configure it to run. The `FilterChain` directive builds a filter chain from smart filters declared, offering the flexibility to insert filters at the beginning or end of the chain, remove a filter, or clear the chain.
## Examples
Server side Includes (SSI)
A simple case of using `mod_filter` in place of `AddOutputFilterByType`
```
FilterDeclare SSI
FilterProvider SSI INCLUDES resp=Content-Type $text/html
FilterChain SSI
```
Server side Includes (SSI)
The same as the above but dispatching on handler (classic SSI behaviour; .shtml files get processed).
```
FilterProvider SSI INCLUDES Handler server-parsed
FilterChain SSI
```
Emulating mod_gzip with mod_deflate
Insert INFLATE filter only if "gzip" is NOT in the Accept-Encoding header. This filter runs with ftype CONTENT_SET.
```
FilterDeclare gzip CONTENT_SET
FilterProvider gzip inflate req=Accept-Encoding !$gzip
FilterChain gzip
```
Image Downsampling
Suppose we want to downsample all web images, and have filters for GIF, JPEG and PNG.
```
FilterProvider unpack jpeg_unpack Content-Type $image/jpeg
FilterProvider unpack gif_unpack Content-Type $image/gif
FilterProvider unpack png_unpack Content-Type $image/png
FilterProvider downsample downsample_filter Content-Type $image
FilterProtocol downsample "change=yes"
FilterProvider repack jpeg_pack Content-Type $image/jpeg
FilterProvider repack gif_pack Content-Type $image/gif
FilterProvider repack png_pack Content-Type $image/png
<Location /image-filter>
FilterChain unpack downsample repack
</Location>
```
## Protocol Handling
Historically, each filter is responsible for ensuring that whatever changes it makes are correctly represented in the HTTP response headers, and that it does not run when it would make an illegal change. This imposes a burden on filter authors to re-implement some common functionality in every filter:
* Many filters will change the content, invalidating existing content tags, checksums, hashes, and lengths.
* Filters that require an entire, unbroken response in input need to ensure they don't get byteranges from a backend.
* Filters that transform output in a filter need to ensure they don't violate a `Cache-Control: no-transform` header from the backend.
* Filters may make responses uncacheable.
`mod_filter` aims to offer generic handling of these details of filter implementation, reducing the complexity required of content filter modules. This is work-in-progress; the `FilterProtocol` implements some of this functionality for back-compatibility with Apache 2.0 modules. For httpd 2.1 and later, the `ap_register_output_filter_protocol`和`ap_filter_protocol` API enables filter modules to declare their own behaviour.
At the same time, `mod_filter` should not interfere with a filter that wants to handle all aspects of the protocol. By default (i.e. in the absence of any `FilterProtocol` directives), `mod_filter` will leave the headers untouched.
At the time of writing, this feature is largely untested, as modules in common use are designed to work with 2.0. Modules using it should test it carefully.
## FilterChain 指令
| [說明](#calibre_link-18) | Configure the filter chain |
| --- | --- |
| [語法](#calibre_link-19) | `FilterChain [+=-@!]filter-name ...` |
| [作用域](#calibre_link-20) | server config, virtual host, directory, .htaccess |
| [覆蓋項](#calibre_link-66) | Options |
| [狀態](#calibre_link-21) | 基本(B) |
| [模塊](#calibre_link-22) | mod_filter |
This configures an actual filter chain, from declared filters. `FilterChain` takes any number of arguments, each optionally preceded with a single-character control that determines what to do:
`+filter-name`
Add filter-name to the end of the filter chain
`@filter-name`
Insert filter-name at the start of the filter chain
`-filter-name`
Remove filter-name from the filter chain
`=filter-name`
Empty the filter chain and insert filter-name
`!`
Empty the filter chain
`filter-name`
Equivalent to `+filter-name`
## FilterDeclare 指令
| [說明](#calibre_link-18) | Declare a smart filter |
| --- | --- |
| [語法](#calibre_link-19) | `FilterDeclare filter-name [type]` |
| [作用域](#calibre_link-20) | server config, virtual host, directory, .htaccess |
| [覆蓋項](#calibre_link-66) | Options |
| [狀態](#calibre_link-21) | 基本(B) |
| [模塊](#calibre_link-22) | mod_filter |
This directive declares an output filter together with a header or environment variable that will determine runtime configuration. The first argument is a filter-name for use in `FilterProvider`, `FilterChain`和`FilterProtocol` directives.
The final (optional) argument is the type of filter, and takes values of `ap_filter_type` - namely `RESOURCE` (the default), `CONTENT_SET`, `PROTOCOL`, `TRANSCODE`, `CONNECTION`或`NETWORK`.
## FilterProtocol 指令
| [說明](#calibre_link-18) | Deal with correct HTTP protocol handling |
| --- | --- |
| [語法](#calibre_link-19) | `FilterProtocol filter-name [provider-name] proto-flags` |
| [作用域](#calibre_link-20) | server config, virtual host, directory, .htaccess |
| [覆蓋項](#calibre_link-66) | Options |
| [狀態](#calibre_link-21) | 基本(B) |
| [模塊](#calibre_link-22) | mod_filter |
This directs `mod_filter` to deal with ensuring the filter doesn't run when it shouldn't, and that the HTTP response headers are correctly set taking into account the effects of the filter.
There are two forms of this directive. With three arguments, it applies specifically to a filter-name and a provider-name for that filter. With two arguments it applies to a filter-name whenever the filter runs _any_ provider.
proto-flags is one or more of
`change=yes`
The filter changes the content, including possibly the content length
`change=1:1`
The filter changes the content, but will not change the content length
`byteranges=no`
The filter cannot work on byteranges and requires complete input
`proxy=no`
The filter should not run in a proxy context
`proxy=transform`
The filter transforms the response in a manner incompatible with the HTTP `Cache-Control: no-transform` header.
`cache=no`
The filter renders the output uncacheable (eg by introducing randomised content changes)
## FilterProvider 指令
| [說明](#calibre_link-18) | Register a content filter |
| --- | --- |
| [語法](#calibre_link-19) | `FilterProvider filter-name provider-name [req|resp|env]=dispatch match` |
| [作用域](#calibre_link-20) | server config, virtual host, directory, .htaccess |
| [覆蓋項](#calibre_link-66) | Options |
| [狀態](#calibre_link-21) | 基本(B) |
| [模塊](#calibre_link-22) | mod_filter |
This directive registers a _provider_ for the smart filter. The provider will be called if and only if the match declared here matches the value of the header or environment variable declared as dispatch.
provider-name must have been registered by loading a module that registers the name with `ap_register_output_filter`.
dispatch argument is a string with optional `req=`, `resp=`或`env=` prefix causing it to dispatch on (respectively) the request header, response header, or environment variable named. In the absence of a prefix, it defaults to a response header. A special case is the word `handler`, which causes `mod_filter` to dispatch on the content handler.
match argument specifies a match that will be applied to the filter's dispatch criterion. The match may be a string match (exact match or substring), a [regex](#calibre_link-67 "see glossary"), an integer (greater, lessthan or equals), or unconditional. The first characters of the match argument determines this:
**First**, if the first character is an exclamation mark (`!`), this reverses the rule, so the provider will be used if and only if the match _fails_.
**Second**, it interprets the first character excluding any leading `!` as follows:
| Character | Description |
| --- | --- |
| _(none)_ | exact match |
| `$` | substring match |
| `/` | regex match (delimited by a second `/`) |
| `=` | integer equality |
| `<` | integer less-than |
| `<=` | integer less-than or equal |
| `>` | integer greater-than |
| `>=` | integer greater-than or equal |
| `*` | Unconditional match |
## FilterTrace 指令
| [說明](#calibre_link-18) | Get debug/diagnostic information from `mod_filter` |
| --- | --- |
| [語法](#calibre_link-19) | `FilterTrace filter-name level` |
| [作用域](#calibre_link-20) | server config, virtual host, directory |
| [狀態](#calibre_link-21) | 基本(B) |
| [模塊](#calibre_link-22) | mod_filter |
This directive generates debug information from `mod_filter`. It is designed to help test and debug providers (filter modules), although it may also help with `mod_filter` itself.
The debug output depends on the level set:
`0` (default)
No debug information is generated.
`1`
`mod_filter` will record buckets and brigades passing through the filter to the error log, before the provider has processed them. This is similar to the information generated by [mod_diagnostics](http://apache.webthing.com/mod_diagnostics/).
`2` (not yet implemented)
Will dump the full data passing through to a tempfile before the provider. **For single-user debug only**; this will not support concurrent hits.
- Apache HTTP Server Version 2.2 文檔 [最后更新:2006年3月21日]
- 版本說明
- 從1.3升級到2.0
- 從2.0升級到2.2
- Apache 2.2 新特性概述
- Apache 2.0 新特性概述
- The Apache License, Version 2.0
- 參考手冊
- 編譯與安裝
- 啟動Apache
- 停止和重啟
- 配置文件
- 配置段(容器)
- 緩沖指南
- 服務器全局配置
- 日志文件
- 從URL到文件系統的映射
- 安全方面的提示
- 動態共享對象(DSO)支持
- 內容協商
- 自定義錯誤響應
- 地址和端口的綁定(Binding)
- 多路處理模塊
- Apache的環境變量
- Apache處理器的使用
- 過濾器(Filter)
- suEXEC支持
- 性能方面的提示
- URL重寫指南
- Apache虛擬主機文檔
- 基于主機名的虛擬主機
- 基于IP地址的虛擬主機
- 大批量虛擬主機的動態配置
- 虛擬主機示例
- 深入研究虛擬主機的匹配
- 文件描述符限制
- 關于DNS和Apache
- 常見問題
- 經常問到的問題
- Apache的SSL/TLS加密
- SSL/TLS高強度加密:緒論
- SSL/TLS高強度加密:兼容性
- SSL/TLS高強度加密:如何...?
- SSL/TLS Strong Encryption: FAQ
- 如何.../指南
- 認證、授權、訪問控制
- CGI動態頁面
- 服務器端包含入門
- .htaccess文件
- 用戶網站目錄
- 針對特定平臺的說明
- 在Microsoft Windows中使用Apache
- 在Microsoft Windows上編譯Apache
- Using Apache With Novell NetWare
- Running a High-Performance Web Server on HPUX
- The Apache EBCDIC Port
- 服務器和支持程序
- httpd - Apache超文本傳輸協議服務器
- ab - Apache HTTP服務器性能測試工具
- apachectl - Apache HTTP服務器控制接口
- apxs - Apache 擴展工具
- configure - 配置源代碼樹
- dbmmanage - 管理DBM格式的用戶認證文件
- htcacheclean - 清理磁盤緩沖區
- htdbm - 操作DBM密碼數據庫
- htdigest - 管理用于摘要認證的用戶文件
- httxt2dbm - 生成RewriteMap指令使用的dbm文件
- htpasswd - 管理用于基本認證的用戶文件
- logresolve - 解析Apache日志中的IP地址為主機名
- rotatelogs - 滾動Apache日志的管道日志程序
- suexec - 在執行外部程序之前切換用戶
- 其他程序
- 雜項文檔
- 與Apache相關的標準
- Apache模塊
- 描述模塊的術語
- 描述指令的術語
- Apache核心(Core)特性
- Apache MPM 公共指令
- Apache MPM beos
- Apache MPM event
- Apache MPM netware
- Apache MPM os2
- Apache MPM prefork
- Apache MPM winnt
- Apache MPM worker
- Apache模塊 mod_actions
- Apache模塊 mod_alias
- Apache模塊 mod_asis
- Apache模塊 mod_auth_basic
- Apache模塊 mod_auth_digest
- Apache模塊 mod_authn_alias
- Apache模塊 mod_authn_anon
- Apache模塊 mod_authn_dbd
- Apache模塊 mod_authn_dbm
- Apache模塊 mod_authn_default
- Apache模塊 mod_authn_file
- Apache模塊 mod_authnz_ldap
- Apache模塊 mod_authz_dbm
- Apache模塊 mod_authz_default
- Apache模塊 mod_authz_groupfile
- Apache模塊 mod_authz_host
- Apache模塊 mod_authz_owner
- Apache模塊 mod_authz_user
- Apache模塊 mod_autoindex
- Apache模塊 mod_cache
- Apache模塊 mod_cern_meta
- Apache模塊 mod_cgi
- Apache模塊 mod_cgid
- Apache模塊 mod_charset_lite
- Apache模塊 mod_dav
- Apache模塊 mod_dav_fs
- Apache模塊 mod_dav_lock
- Apache模塊 mod_dbd
- Apache模塊 mod_deflate
- Apache模塊 mod_dir
- Apache模塊 mod_disk_cache
- Apache模塊 mod_dumpio
- Apache模塊 mod_echo
- Apache模塊 mod_env
- Apache模塊 mod_example
- Apache模塊 mod_expires
- Apache模塊 mod_ext_filter
- Apache模塊 mod_file_cache
- Apache模塊 mod_filter
- Apache模塊 mod_headers
- Apache模塊 mod_ident
- Apache模塊 mod_imagemap
- Apache模塊 mod_include
- Apache模塊 mod_info
- Apache模塊 mod_isapi
- Apache模塊 mod_ldap
- Apache模塊 mod_log_config
- Apache模塊 mod_log_forensic
- Apache模塊 mod_logio
- Apache模塊 mod_mem_cache
- Apache模塊 mod_mime
- Apache模塊 mod_mime_magic
- Apache模塊 mod_negotiation
- Apache模塊 mod_nw_ssl
- Apache模塊 mod_proxy
- Apache模塊 mod_proxy_ajp
- Apache模塊 mod_proxy_balancer
- Apache模塊 mod_proxy_connect
- Apache模塊 mod_proxy_ftp
- Apache模塊 mod_proxy_http
- Apache模塊 mod_rewrite
- Apache模塊 mod_setenvif
- Apache模塊 mod_so
- Apache模塊 mod_speling
- Apache模塊 mod_ssl
- Apache模塊 mod_status
- Apache模塊 mod_suexec
- Apache模塊 mod_unique_id
- Apache模塊 mod_userdir
- Apache模塊 mod_usertrack
- Apache模塊 mod_version
- Apache模塊 mod_vhost_alias
- Developer Documentation for Apache 2.0
- Apache 1.3 API notes
- Debugging Memory Allocation in APR
- Documenting Apache 2.0
- Apache 2.0 Hook Functions
- Converting Modules from Apache 1.3 to Apache 2.0
- Request Processing in Apache 2.0
- How filters work in Apache 2.0
- Apache 2.0 Thread Safety Issues
- 詞匯和索引
- 詞匯表
- 指令索引
- 指令速查
- 模塊索引
- 站點導航