首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Lua示例-获取“routeIP”:运行时错误:/etc/haproxy/fetch_req.lua:3:尝试从/etc/haproxy/fetch_req.lua调用一个零值(方法'fhdr')

Lua示例-获取“routeIP”:运行时错误:/etc/haproxy/fetch_req.lua:3:尝试从/etc/haproxy/fetch_req.lua调用一个零值(方法'fhdr')
EN

Stack Overflow用户
提问于 2022-05-18 18:49:10
回答 1查看 132关注 0票数 0

我试图使用HAProxy中的LUA脚本打印我的“X转发-for”头。但我错了

/var/log/haproxy.log

代码语言:javascript
复制
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转发-for头。

代码语言:javascript
复制
#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]

打印相同route_req.lua的Lua脚本

代码语言:javascript
复制
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转发头没有设置呢?

据我所知,这个字段还包含最后一个设备的IP地址,它转发了我的请求,所以我不能只使用src。

提供连接IP地址的列表。

负载均衡器将来自传入请求的X转发-For字段附加上最后一个远程对等地址。附加地址前面有逗号和空格。如果客户端请求头不包括X转发-For字段,则此值等于Forwarded值.

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-05-18 22:08:53

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72294523

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档