mod_substitute
描述: | 在响应主体上执行搜索和替换操作 |
状态: | 延期 |
模块标识符: | substitute_module |
源文件: | mod_substitute.c |
兼容性: | 可在 Apache HTTP Server 2.2.7 及更高版本中使用 |
摘要
mod_substitute提供了一种在响应主体上执行正则表达式和固定 string 替换的机制。
替代指令
描述: | Pattern 过滤响应内容 |
句法: | Substitute s/pattern/substitution/[infq] |
Context: | 目录,.htaccess |
覆盖: | FileInfo |
状态: | 延期 |
模块: | mod_substitute |
Substitute
指令指定搜索并替换 pattern 以应用于响应主体。
可以使用以下标志的任意组合来修改 pattern 的含义:
i
执行 case-insensitive match。n
默认情况下,pattern 被视为正则表达式。使用 n flag 强制 pattern 被视为固定的 string。f
f flag 使 mod_substitute 平坦化替换的结果,允许稍后的替换发生在该边界上。这是默认值。q
q flag 导致 mod_substitute 在每次替换后不会使桶变平。这可以导致更快的响应和 memory 利用率的降低,但是只有在一个替换的结果不可能匹配后一个的模式或正则表达式时才应该使用。
替换可能包含文字文本和正则表达式反向引用
例
<Location "/"> AddOutputFilterByType SUBSTITUTE text/html Substitute "s/foo/bar/ni" </Location>
用于分隔(或“分隔”)替换 string 的各个部分的字符被称为“分隔符”,并且最常见的是为此目的使用斜杠。
如果 pattern 或替换包含斜杠字符,则可以使用替代分隔符使指令更具可读性:
使用备用分隔符的示例
<Location "/"> AddOutputFilterByType SUBSTITUTE text/html Substitute "s|<BR */?>||i" </Location>
当使用正则表达式时,可以在比较和替换中使用反向引用,如下面的示例所示:
使用反向引用和捕获的示例
<Location "/"> AddOutputFilterByType SUBSTITUTE text/html # "foo=k,bar=k" -> "foo/bar=k" Substitute "s|foo=(w+),bar=1|foo/bar=$1|" </Location>
mod_substitute
的 common 使用场景是 front-end 服务器代理对 back-end 服务器的请求的情况,该服务器返回带有引用 back-end 服务器的 hard-coded 嵌入 URL 的 HTML。这些 URL 不适用于 end-user,因为 back-end 服务器无法访问。
在这种情况下,mod_substitute
可用于将这些 URL 编写为可从前端起作用的内容:
重写代理内容中嵌入的 URL
ProxyPass "/blog/" "http://internal.blog.example.com/" ProxyPassReverse "/blog/" "http://internal.blog.example.com/" Substitute "s|http://internal.blog.example.com/|http://www.example.com/blog/|i"
ProxyPassReverse修改由 back-end 服务器发送的任何Location
(重定向)headers,并且在此 example 中,Substitute
通过修复 HTML 响应来处理问题的 rest。
SubstituteInheritBefore 指令
描述: | 更改继承模式的 merge order |
句法: | SubstituteInheritBefore on\|off |
默认: | SubstituteInheritBefore off |
Context: | 目录,.htaccess |
覆盖: | FileInfo |
状态: | 延期 |
模块: | mod_substitute |
兼容性: | 可在 httpd 2.4.17 及更高版本中使用 |
是先应用继承的替代模式(on
),还是应用当前 context(off
)之后的模式。SubstituteInheritBefore
本身是继承的,因此继承它的上下文(那些没有指定自己的SubstituteInheritBefore
value 的上下文)将应用最接近定义的 merge order。
SubstituteMaxLineLength 指令
描述: | 设置最大 line 大小 |
句法: | SubstituteMaxLineLength bytes(b\|B\|k\|K\|m\|M\|g\|G) |
默认: | SubstituteMaxLineLength 1m |
Context: | 目录,.htaccess |
覆盖: | FileInfo |
状态: | 延期 |
模块: | mod_substitute |
兼容性: | 可在 httpd 2.4.11 及更高版本中使用 |
mod_substitute处理的最大 line 大小仅限于限制 memory 使用。可以使用SubstituteMaxLineLength
配置限制。 value 可以作为字节数给出,并且可以用单个字母b
,B
,k
,K
,m
,M
,g
,G
作为后缀,以分别提供字节,千字节,兆字节或千兆字节的大小。
例
<Location "/"> AddOutputFilterByType SUBSTITUTE text/html SubstituteMaxLineLength 10m Substitute "s/foo/bar/ni" </Location>