HAproxy lua支持:想从Lua文件调用core.action方法

我是haproxy集成的新手 我想为每个http请求调用一个操作

test_world.lua文件代码

core.register_action("test_world", { "tcp-req", "http-req" },function(txn)
  txn:Info("Hello world")
end)

haproxy.cfg文件代码

global
 lua-load /usr/local/etc/haproxy/test_world.lua

defaults
  mode http

frontend  http
  bind 0.0.0.0:80
  mode http
  http-request  lua.test_world

我正在从浏览器和telnet发出请求,但没有得到响应

点赞
用户7631780
用户7631780

你正在使用错误的“hello world”例子。

在你的例子里,你将一个 Lua 函数注册为一个 action

注册为 actions 的函数更常用于操作 HTTP 请求,而不是发送 HTTP 响应。

如果你需要发送响应而不是发送到后端服务器,你需要使用一个 applet,就像下面的例子:

core.register_service("hello-world", "http", function(applet)
 local response = "Hello World !"
 applet:set_status(200)
 applet:add_header("content-length", string.len(response))
 applet:add_header("content-type", "text/plain")
 applet:start_response()
 applet:send(response)
end)
2017-09-11 16:15:17