【问题标题】:Call lua in response to 503 in haproxy在haproxy中调用lua响应503
【发布时间】:2019-05-17 12:03:53
【问题描述】:

我正在使用 HAProxy Lua API。我想在 all 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

这工作正常,handle-requesthandle-response 被调用,即使后端服务器返回 500 状态或其他错误。

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

我也尝试过使用 TCP 操作。

在这种情况下有什么方法可以运行 Lua 代码吗?

【问题讨论】:

    标签: lua haproxy


    【解决方案1】:

    我能找到的唯一方法是绝对不应该使用的非常可怕和邪恶的黑客攻击。

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

    配置:

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

    卢阿:

    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)
              -- Look for your 503 and do stuff
          else
            core.sleep(1)
          end
          core.yield()
      end
    end)
    

    再次强调,这是一个多么糟糕的主意。

    【讨论】:

      猜你喜欢
      • 2019-04-13
      • 2020-06-04
      • 2013-08-18
      • 2017-07-12
      • 2012-12-09
      • 2015-09-22
      • 1970-01-01
      • 2012-02-08
      • 1970-01-01
      相关资源
      最近更新 更多