【发布时间】: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