【问题标题】:Lua sample-fetch 'routeIP': runtime error: /etc/haproxy/route_req.lua:3: attempt to call a nil value (method 'fhdr') from /etc/haproxy/route_req.luaLua sample-fetch \'routeIP\':运行时错误:/etc/haproxy/route_req.lua:3: 尝试从 /etc/haproxy/route_req.lua 调用 nil 值(方法 \'fhdr\')
【发布时间】:2022-05-19 06:08:53
【问题描述】:

我试图在 HAProxy 中使用 LUA 脚本打印我的“X-forwarded-for”标头。但我收到错误

/var/log/haproxy.log

May 18 18:37:06 ubuntu-s-1vcpu-1gb-blr1-01 haproxy[161927]: [ALERT] 137/183706 (161927) : Lua sample-fetch 'routeIP': runtime error: /etc/haproxy/route_req.lua:3: attempt to call a nil value (method 'fhdr') from /etc/haproxy/route_req.lua:3 C function line 1.
May 18 18:37:07 ubuntu-s-1vcpu-1gb-blr1-01 haproxy[161927]: [ALERT] 137/183707 (161927) : Lua sample-fetch 'routeIP': runtime error: /etc/haproxy/route_req.lua:3: attempt to call a nil value (method 'fhdr') from /etc/haproxy/route_req.lua:3 C function line 1.
 Lua sample-fetch 'routeIP': runtime error: /etc/haproxy/route_req.lua:3: attempt to call a nil value (method 'fhdr') from /etc/haproxy/route_req.lua:3 C function line 1.

这是我的 haproxy.cfg 文件,我在其中设置 X-forwarded-for 标头。

#HAProxy for web servers
frontend web-frontend
  bind 10.122.0.2:80
  bind 139.59.75.106:80
  mode http

  http-request set-header X-Forwarded-Proto https if { ssl_fc } # For Proto
  http-request add-header X-Real-Ip %[src] # Custom header with src IP
  option forwardfor # X-forwarded-for

  use_backend %[lua.routeIP]

我打印相同的 Lua 脚本route_req.lua

local function getIP(txn)
        local clientip = txn.f:src()
        local src = txn.f:fhdr("x-forwarded-for");

        core.log(core.info, "ClientP and XForwardedFor header : " .. clientip .. " - " .. src)
        // My code goes here

end

core.register_fetches('routeIP', getIP)

我到底哪里出错了为什么没有设置 X-forwarded-for 标头?

据我了解,此字段还包含转发我的请求的最后一个设备的 IP 地址,因此我不能只使用 src。

提供连接 IP 地址列表。

负载均衡器将最后一个远程对等地址附加到 来自传入请求的 X-Forwarded-For 字段。逗号和空格 在附加地址之前。如果客户端请求头没有 包含一个 X-Forwarded-For 字段,该值等于 X-Real-IP 价值。

【问题讨论】:

    标签: lua haproxy distributed-system system-design x-forwarded-for


    【解决方案1】:

    该错误意味着txn.f 不包含名为fhdr 的方法。也许您的意思是req_fhdr

    【讨论】:

    • 我用 req_fhdr 试过了,没用
    • @HimanshuPoddar“没用”是什么意思?究竟发生了什么?
    • 这意味着我仍然遇到同样的错误Lua sample-fetch 'routeIP': runtime error: /etc/haproxy/route_req.lua:5: attempt to concatenate a nil value (local 'src') from /etc/haproxy/route_req.lua:5 C function line 1.
    • @HimanshuPoddar 这是一个不同的错误,所以这意味着做过工作。您的新问题是您没有正确处理未设置标题的情况。
    • 我不明白的是,当我清楚地使用option forwardfor 时,为什么没有设置这个标题
    猜你喜欢
    • 1970-01-01
    • 2017-04-03
    • 2011-07-30
    • 1970-01-01
    • 2018-05-30
    • 2019-09-15
    • 2019-01-06
    • 2015-06-15
    • 2020-03-27
    相关资源
    最近更新 更多