用清漆和正则表达式替换页面中的内容

twe*_*ak2 5 regex webserver caching varnish varnish-vcl

如果我想让我的清漆缓存服务器在服务或存储页面(vcl_fetch?)之前从后端替换页面内的内容(即:更改div上的类),该怎么做?

我想使用简单的正则表达式来执行替换,因为我想它本身在清漆中受支持。

Ket*_*ola 6

Varnish 本身不支持修改响应正文。为此,您需要一个 Varnish 模块 (vmod)。

Aivars Kalvans 有libvmod-rewrite,它完全符合您的要求。然而,vmod 是一个概念证明,根据 Aivars 的说法,它还没有准备好用于生产。在任何情况下,您都可以将其用作起点。

如果您使用的是 Apache,您可以使用mod_ext_filter来修改响应正文。这是 mod_ext_filters 文档中的一个示例。由于您可以将响应正文传递给任何外部命令,因此对内容进行必要的修改非常容易。

# mod_ext_filter directive to define a filter which
# replaces text in the response
#
ExtFilterDefine fixtext mode=output intype=text/html cmd="/bin/sed s/verdana/arial/g"

<Location />
# core directive to cause the fixtext filter to
# be run on output
SetOutputFilter fixtext
</Location> 
Run Code Online (Sandbox Code Playgroud)