--- title: "9. 支持的过滤器" linkTitle: "9. 过滤器" weight: 200 description: "跟踪、压缩、SPOE、缓存、FastCGI、OpenTracing、以及带宽过滤器" icon: fa-solid fa-layer-group module: [HAPROXY] categories: [参考] aliases: - /haproxy/configuration/filters/ - /docs/haproxy/configuration/filters/ - /haproxy/filters/ upstream_link: "https://docs.haproxy.org/3.4/configuration.html" upstream_name: "HAProxy 3.4 Configuration Manual" upstream_ref: "v3.4.4, chapter 9" --- 以下是官方支持的过滤器及其所接受的参数列表。根据编译选项的不同,部分过滤器可能不可用。可用过滤器的列表将在 HAProxy -vv. 中报告。 另请参见:"过滤器" ## 9.1. 跟踪 {#section-9-1} 过滤器 trace [name ``] [random-forwarding] [max-fwd ``] [hexdump] 参数: ```text is an arbitrary name that will be reported in messages. If no name is provided, "TRACE" is used. inhibits trace messages. enables the random forwarding of parsed data. By default, this filter forwards all previously parsed data. With this parameter, it only forwards a random amount of the parsed data. is the maximum amount of data that can be forwarded at a time. "max-fwd" option can be combined with the random forwarding. must be an positive integer. 0 means there is no limit. dumps all forwarded data to the server and the client. ``` 此过滤器可作为开发新过滤器的基础。它定义了所有回调函数,并在标准错误流(stderr)中输出有用信息,供所有回调函数参考。该过滤器可用于调试其他过滤器的活动,或简单地用于排查 HAProxy 的运行状态。 使用 `` 和/或 `` 参数是测试过滤器行为的有效方法,该过滤器用于解析客户端与服务器之间交换的数据,并通过在处理过程中引入延迟来实现。 ## 9.2. HTTP 压缩 {#section-9-2} 过滤器 comp-req 启用根据“compression”设置显式尝试压缩 HTTP 请求的过滤器。 隐式设置“compression direction request”。 过滤器 comp-res 启用根据“compression”设置显式尝试压缩 HTTP 响应的过滤器。 隐式设置“compression direction response” filter compression(已弃用) 用于向后兼容的别名,其功能等同于同时启用 "comp-req" 和 "comp-res" 过滤器。必须使用 "compression" 关键字来配置相应行为: 在 HAProxy 1.7 中,HTTP 压缩已移入过滤器模块。仍需使用 "compression" 关键字来启用和配置 HTTP 压缩。当未使用其他过滤器时,仅此一项已足够。当与缓存或 fcgi-app 启用时,同样仅此一项已足够。此时,压缩始终在响应存储至缓存后执行。但当同一监听器/前端/后端启用至少一个除缓存或 fcgi-app 以外的过滤器时,必须显式使用过滤器行来启用 HTTP 压缩。这一点需注意过滤器的评估顺序。 另请参阅:“压缩”,[第 9.4 节](/zh/docs/haproxy/filters/#section-9-4) 中关于缓存过滤器的内容,以及[第 9.5 节](/zh/docs/haproxy/filters/#section-9-5) 中关于 fcgi-app 过滤器的内容。 ## 9.3. 流处理卸载引擎 (SPOE) {#section-9-3} 过滤器 spoe [引擎 ``] 配置 `` 参数: ```text is the engine name that will be used to find the right scope in the configuration file. If not provided, all the file will be parsed. is the path of the engine configuration file. This file can contain configuration of several engines. In this case, each part must be placed in its own scope. ``` 流处理卸载引擎(SPOE)是一种与外部组件通信的过滤器。它允许在分层应用中将部分流的特定处理任务卸载至外部组件。这些外部组件及其与之交换的信息主要通过专用配置文件进行配置。此外,还需在 HAProxy 配置中定义专用后端。 SPOE 通过自研的二进制协议——流处理卸载协议(Stream Processing Offload Protocol,SPOP),与外部组件通信。 当 SPOE 在流中使用时,会启动一个专用流来处理与外部组件的通信。主流是该“SPOE”流的父流。这意味着可以从“SPOE”流中获取主流的变量。有关变量的详细信息,请参见 [第 2.8 节](/zh/docs/haproxy/configuration-basics/#section-2-8)。 有关 SPOE 配置和 SPOP 规范的全部信息,请参见“doc/SPOE.txt”。 ## 9.4. 缓存 {#section-9-4} 过滤器 缓存 `` 参数: ```text is name of the cache section this filter will use. ``` 缓存通过过滤器存储可缓存的响应。必须使用 HTTP 规则 `cache-store` 和 `cache-use` 来定义缓存的使用方式和时机。默认情况下,相应的过滤器会隐式定义。当仅使用 `fcgi-app` 或压缩过滤器时,无需额外配置即可满足需求。在此情况下,压缩过滤器始终在缓存过滤器之后执行。但当同一监听器/前端/后端使用了除压缩或 `fcgi-app` 以外的其他过滤器时,必须显式添加过滤器行以启用缓存。这一点需特别注意,因为过滤器的执行顺序至关重要。 有关更多信息,请参见:[第 9.2 节](/zh/docs/haproxy/filters/#section-9-2) 关于压缩过滤器,[第 9.5 节](/zh/docs/haproxy/filters/#section-9-5) 关于 FCGI 应用过滤器,以及[第 6 节](/zh/docs/haproxy/cache/) 关于缓存。 ## 9.5. FastCGI 应用 {#section-9-5} 过滤器 fcgi-app `` 参数: ```text is name of the fcgi-app section this filter will use. ``` FastCGI 应用程序使用过滤器对请求路径上的所有自定义参数进行评估,并对响应路径上的头进行处理。`` 必须引用一个已存在的 fcgi-app 段。应使用指令 "use-fcgi-app" 来定义所使用的应用程序。默认情况下,相应的过滤器会隐式定义。当仅使用缓存或压缩过滤器时,此方式已足够。但当同一后端使用至少一个除压缩或缓存以外的其他过滤器时,必须显式使用过滤器行指定 fcgi-app。这一点对于了解过滤器的评估顺序至关重要。 另请参阅:“use-fcgi-app”,[第 9.2 节](/zh/docs/haproxy/filters/#section-9-2) 关于压缩过滤器,[第 9.4 节](/zh/docs/haproxy/filters/#section-9-4) 关于缓存过滤器,以及[第 10 节](/zh/docs/haproxy/fastcgi/) 关于 FastCGI 应用。 ## 9.6. OpenTracing {#section-9-6} 本文档中的 OpenTracing 过滤器为 HAProxy 提供了对分布式追踪的原生支持。通过向受支持的追踪器(如 Datadog、Jaeger、Lightstep 和 Zipkin)之一发送符合 OpenTracing 规范的请求,即可启用该功能。请注意:所列追踪器不按优先级排序,而是按字母顺序排列。 此功能仅在 HAProxy 编译时启用 USE_OT=1 时才可用。 通过在 HAProxy 配置中显式指定,可激活 OpenTracing 过滤器。 若未执行此操作,OpenTracing 过滤器将完全不参与 HAProxy 的工作。 过滤器 opentracing [id ``] 配置 `` 参数: ```text is the OpenTracing filter id that will be used to find the right scope in the configuration file. If no filter id is specified, 'ot-filter' is used as default. If scope is not specified in the configuration file, it applies to all defined OpenTracing filters. is the path of the OpenTracing configuration file. The same file can contain configurations for multiple OpenTracing filters simultaneously. In that case we do not need to define scope so the same configuration applies to all filters or each filter must have its own scope defined. ``` 有关过滤器操作、配置和使用的更详细文档,请参见 addons/ot 目录。 请注意:由于 OpenTracing 本身已不再由其作者维护或支持,因此不建议在新设计中使用 OpenTracing 过滤器。OpenTracing 将在 3.3 版本中弃用,并在 3.5 版本中移除。自 3.4 版本起,已提供基于 OpenTelemetry 的替代过滤器,完整的构建说明目前位于: ```text https://github.com/haproxytech/haproxy-opentelemetry/ ``` ## 9.7. 带宽限制 {#section-9-7} 过滤器 bwlim-in `` 默认限速 `` 默认周期 `