【问题标题】:Varnish VCL simply replace client.ip for req.http.x-forwarded-forVarnish VCL 只需将 client.ip 替换为 req.http.x-forwarded-for
【发布时间】:2016-08-28 07:17:57
【问题描述】:

在我的 Varnish 2 设置中,我有一个像这样的清除/禁止块:

acl purge {
    "localhost";
    "x.x.x.x"/24;
}

sub vcl_recv {
    if (req.request == "PURGE") {
        if (!client.ip ~ purge) {
            error 405 "Not allowed.";
        }
        return (lookup);
    }
    if (req.request == "BAN") {
        if (!client.ip ~ purge) {
            error 405 "Not allowed.";
        }
        ban("obj.http.x-host == " +req.http.host+" && obj.http.x-url ~ "+req.url);

        # Throw a synthetic page so the
        # request wont go to the backend.
        error 200 "Ban added";
    }
}

我希望我可以简单地替换 req.http.x-forwarded-for 的 if 语句中的 client.ip,但是当我这样做时会出现以下编译错误:

Message from VCC-compiler:
Expected CSTR got 'purge'
(program line 944), at
('purging-banning.vcl' Line 16 Pos 41)
    if (!req.http.x-forwarded-for ~ purge) {

----------------------------------------#####----

Running VCC-compiler failed, exit 1
VCL compilation failed

我一直在搜索 Google 和 StackOverflow,但我还没有找到解决我的问题的好方法,或者 req.http.x-forwarded-for 不会出现在此处的正确位置的原因。

谁能帮忙?

【问题讨论】:

  • 您的示例似乎与错误不匹配,您似乎只有“client.ip”。但是,只是说明 client.ip 是一个实际的 IP 对象,而 req.http.x-forwarded-for 是一个字符串。 purge 基本上是一个 IP 映射列表,是一个特殊的 Varnish 对象
  • 谢谢!确实很有趣:)

标签: varnish varnish-vcl x-forwarded-for


【解决方案1】:

尝试使用 vmod_std 中的“ip”。见:https://varnish-cache.org/docs/trunk/reference/vmod_std.generated.html#func-ip

像这样:

if (std.ip(req.http.x-forwarded-for, "0.0.0.0") !~ purge) {
    error 405 "Not allowed.";
}

这只是将字符串对象转换为 IP 对象。然后,可以将 IP 对象与 IP acl 列表进行比较。

【讨论】:

  • 谢谢。我目前无法尝试这个(我原来的帖子是一年多前的),所以我不能将你的答案标记为“答案”。但是,我希望您的回答可以帮助其他人 - 或者我 - 将来。如果我能够在某一时刻对此进行测试,我会通知您。
【解决方案2】:

我没有“代表”可以发表评论,因此我尝试使用std.ip 确认答案。我有同样的情况,使用std.ip 修复了它。记得在你的 default.vcl 中添加import std

另外,在我的例子中,nginx 转发到清漆,而 X-Forwarded-For 有时有 2 个 IP,所以我使用 X-Real-IP,它在我的 nginx 转发配置中设置为 $remote_addr

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-21
    • 2015-04-26
    • 1970-01-01
    • 1970-01-01
    • 2014-03-03
    • 1970-01-01
    • 2014-10-22
    • 1970-01-01
    相关资源
    最近更新 更多