如果文档中的示例正确,我正在尝试使用作为 Varnish 4.0一部分的 std.ip
返回客户端IP,该客户端IP应该是 X-Forwarded-For header 中的第一个有效IP地址。
varnishtest "Test v4 vcl X-Forwarded-For Header logic"
server s1 {
rxreq
expect req.http.X-Real-IP == "2.1.1.1"
expect req.http.X-Forwarded-For == "2.1.1.1, 3.3.3.3, 3.3.3.3, 127.0.0.1"
txresp
} -start
varnish v1 -vcl+backend {
sub vcl_recv {
set req.http.X-Real-IP = std.ip(req.http.X-Forwarded-For, "0.0.0.0");
}
} -start
client c1 {
txreq -url "/" -hdr "X-Forwarded-For: 2.1.1.1, 3.3.3.3, 3.3.3.3"
rxresp
}
client c1 -run
上面的死是丑陋的:
...
*** v1 0.9 debug| Assert error in vwk_thread(), waiter/cache_waiter_kqueue.c line 115:\n
*** v1 0.9 debug| Condition(read(vwk->pipe[0], &c, 1) == 1) not true.\n
...
并且无法通过返回第一个IP地址来表现我想要的行为。
更新
或者,我发现以下内容确实可以达到相同的目的,但是仍然无法使
std.ip
正常工作:varnish v1 -vcl+backend {
sub vcl_recv {
set req.http.X-Real-IP = regsub(req.http.X-Forwarded-For, "\s*,.*$", "");
}
} -start
作为后续问题。这个
vcl_recv{...}
逻辑实际上存在于我的default.vcl
文件中,在其中定义了我的所有后端,探针。但是,当我尝试通过将其包含到varnishtest文件中来测试该代码时,如下所示:varnish v1 -vcl+backend {
include "/path/to/file.vcl";
} -start
该测试未在服务器
expect
中获取s1
语句。如果有人可以在以下方面给我一些澄清,我会大为震惊:std.ip(req.http.X-Forwarded-For, "0.0.0.0")
的行为不符合预期? default.vcl
中使用Expect语句测试包含的s1
? 谢谢你。
最佳答案
您需要添加行
import std;
在vcl文件的顶部
关于http-headers - Varnish :如何使用 `std.ip()` 设置标题值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36671561/