当用户请求http://sub1.example.com/a/b/c时,我需要将该请求的主机名头替换为example.com,并重写到http://example.com/sub1/a/b/c的路径。我试着使用reqrep,但找不到如何将子域部件添加到路径中。
reqirep ^Host:\ (.*[^\.])(\.example\.com) Host:\ example.com
reqrep ^([^\ :]*)\ (.*) \1\ /???/\2我找到了%Req.hdr(主机),较低的字段(1,‘.’)方法来获取子域名,但是我不能在reqrep的替换部分中使用它。如何在这部分中使用变量?
发布于 2016-11-18 03:32:42
将子域捕获到一个名为req.rewrite_example的请求变量中(这是我刚刚创建的一个名称.req.是必需的,其余的可以是你想要的).为此,捕获Host:头的值,将其转换为小写,然后使用regsub()转换器在最后清除.example.com。只有当主机名以.example.com结尾时才这样做--否则变量是未定义的,后续规则中的-m found测试将是假的,并且这些规则不会触发(对于我们可能看到的任何其他域名,这就是我们想要的)。
http-request set-var(req.rewrite_example) req.hdr(host),lower,regsub(\.example\.com$,) if { hdr_end(host) -i .example.com }如果定义了变量,则在路径的开头插入捕获的变量(如果第一个规则是匹配的,则应该是该变量)。
http-request set-path /%[var(req.rewrite_example)]%[path] if { var(req.rewrite_example) -m found }如果定义了变量,则将Host:头替换为文字字符串example.com。
http-request set-header Host example.com if { var(req.rewrite_example) -m found }...send请求..。
curl http://www.example.com/tacgnol.jpg...and将向后端发送以下标头:
GET /www/tacgnol.jpg HTTP/1.1
User-Agent: curl/7.35.0
Accept: */*
Host: example.com好了。
https://serverfault.com/questions/815484
复制相似问题