我试图使用HAProxy中的LUA脚本打印我的“X转发-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转发-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]打印相同route_req.lua的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转发头没有设置呢?
据我所知,这个字段还包含最后一个设备的IP地址,它转发了我的请求,所以我不能只使用src。
提供连接IP地址的列表。
负载均衡器将来自传入请求的X转发-For字段附加上最后一个远程对等地址。附加地址前面有逗号和空格。如果客户端请求头不包括X转发-For字段,则此值等于Forwarded值.
发布于 2022-05-18 22:08:53
该错误意味着txn.f不包含名为fhdr的方法。也许你是说req_fhdr。
https://stackoverflow.com/questions/72294523
复制相似问题