【问题标题】:nginx behind haproxy behind varnishnginx 背后的 haproxy 背后的清漆
【发布时间】:2013-08-09 11:50:52
【问题描述】:

我的 nginx 服务器在 haproxy 后面,它在 Varnish 后面:

请求 web => Varnish => HaProxy => Nginx

我的问题是我无法检索客户端的 IP 地址,在 nginx 日志中我只有 haproxy 的 IP 地址,所以我认为我的 XForwardfor 不好。

这是我为 Varnish 写的:

remove req.http.X-Forwarded-For;
set    req.http.X-Forwarded-For = req.http.rlnclientipaddr;

这是我在 haproxy 上放的:

选项转发

在 nginx 中我已经配置了 http_real_ip_module 并且我有:

set_real_ip_from 192.168.1.2; real_ip_header X-Forwarded-For;

谢谢

PS:如果我删除 Varnish,并将 Haproxy 放在端口 80 上,我就有了真实的 IP 地址。

【问题讨论】:

    标签: nginx header varnish haproxy


    【解决方案1】:

    Haproxy 似乎并没有真正使用 x-forwarded-for 标头。看来它只是替换它。如果您在 1.5 的更高版本(我认为是 17 或更高版本)上运行,那么您实际上可以进行变量连接,这意味着您可以自己设置 x-forwarded-for 标头,而无需使用选项 forwardfor。我在一个非常大的 haproxy 实现中执行此操作,并且运行良好。

    另一个选项是更改 haproxy 选项 forwardfor 标头以使用不同的标头。这意味着在 nginx 服务器上,您必须查看两个标头。来自 varnish 的一个将具有最终用户 IP 地址,来自 haproxy 的一个将具有 varnish 服务器的 IP 地址。为此,haproxy 配置如下所示:

    option forwardfor header varnish-x-forwarded-for
    

    【讨论】:

    • 是的!你让我开心,谢谢。它适用于自定义转发。
    【解决方案2】:

    确保 Varnish 设置了正确的标题。

    在 Varnish 中设置 X-Forwarded-For 的实际代码应如 default.vcl [1] 中所述:

    sub vcl_recv {
      if (req.restarts == 0) {
        if (req.http.x-forwarded-for) {
          set req.http.X-Forwarded-For =
              req.http.X-Forwarded-For + ", " + client.ip;
        } else {
          set req.http.X-Forwarded-For = client.ip;
        }
      }
      # ...
    }
    

    [1]https://www.varnish-cache.org/docs/3.0/reference/vcl.html#examples

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-12
      相关资源
      最近更新 更多