使用 Lua 在 haproxy 响应 503 时调用

我正在使用 HAProxy Lua API。我想在所有的 http-request 和 http-response 事件上调用 Lua 操作。

这个方法在没有后端可用时是有效的,但 HAProxy 自身不能完成请求。在这种情况下,我找不到调用 Lua 的方法。

例如

listen appname
    bind 0.0.0.0:8000
    mode http
    http-request lua.handle-request
    http-response lua.handle-response
    server hostname 127.0.0.1:8001 check

这个方法很有效,即使后端服务器返回一个 500 状态或其他错误,handle-requesthandle-response 也会被调用。

但是,如果 HAProxy 本身找不到后端(自己返回 503),则会调用 handle-request,但不会调用 handle-response

我也尝试过使用 TCP 操作。

在这种情况下,有没有办法运行 Lua 代码?

点赞
用户10799760
用户10799760

我找到的唯一方法是一个非常可怕和邪恶的黑客技巧,不应该使用。

使用 task 从 syslog 中读取 UDP 来捕获 HAproxy 日志中的 503 响应。

配置:

lua-load youre_a_terrible_person.lua
log 127.0.0.1:8004 local0 debug
option httplog

Lua:

core.register_task(function()
  local socket = require("socket")

  udp = socket.udp()
  udp:setsockname("*", 8004)
  udp:settimeout(0)

  while true do
      data = udp:receive()
      if data then
          print("Received: ", data)
          -- 寻找 503 并执行相应操作
      else
        core.sleep(1)
      end
      core.yield()
  end
end)

再次强调这是多么糟糕的主意,不应该使用。

2018-12-20 05:10:14