当用户请求http://sub1.example.com/a/b/c时,我需要将此请求的主机名头replace为example.com,并将path重写为http://example.com/sub1/a/b/ c 。 我试图使用reqrep,但我无法find如何将子域部分添加到path。
reqirep ^Host:\ (.*[^\.])(\.example\.com) Host:\ example.com reqrep ^([^\ :]*)\ (.*) \1\ /???/\2
我find%[req.hdr(host),lower,field(1,'。')]方法来获取子域名,但是我不能在reqrep方法的replace部分中使用它。 如何在这部分使用variables?
将该子域捕获到一个名为req.rewrite_example的请求variables中(这是我刚才req.rewrite_example一个名称… req.是必需的,剩下的可以是任何你想要的)…通过捕获Host:的值来实现Host:头,将其转换为小写,然后使用regsub()转换器淘汰.example.com结尾。 只有当主机名以.example.com结尾 – 否则variables是未定义的,并且在随后的规则中-m found的-m foundtesting将是错误的,这些规则不会触发(这是我们想要的,对于任何其他域名我们可能会看到)。
http-request set-var(req.rewrite_example) req.hdr(host),lower,regsub(\.example\.com$,) if { hdr_end(host) -i .example.com }
如果variables已定义,则将捕获的variables插入path的开始位置(如果第一个规则匹配,则应该是这个variables)。
http-request set-path /%[var(req.rewrite_example)]%[path] if { var(req.rewrite_example) -m found }
如果定义了variables,则用文本stringexample.comreplaceHost:头。
http-request set-header Host example.com if { var(req.rewrite_example) -m found }
发送请求…
curl http://www.example.com/tacgnol.jpg
…以下头文件被发送到后端:
GET /www/tacgnol.jpg HTTP/1.1 User-Agent: curl/7.35.0 Accept: */* Host: example.com
完成。