On Fri, 22 May 2015 19:06:59 +0000 (UTC)
Mrunmayi Dhume <[email protected]> wrote:
> Hello,
> I am using haproxy-1.6 with Lua. I have a use-case where I want to set the
> destination (backend server) very dynamically, based on certain layer 7
> information (I am trying to avoid updating haproxy configuration and make it
> complicated with a ~100 domain names and their corresponding backend server
> name and do a map lookup, I want lua to do it )
> eg: from lua, based on certain layer 7 information I want to change the
> backend server (set destination) it should connect to..
> if Host is ‘example.test.com’; then backend is test1.server.com elsif Host
> is ‘example2.test.com; then backend is test2.server.com I want to keep the
> haproxy config simple and have only the Listen directive listed and take more
> control of the request flow from the LUA code.
> The ATS traffic server does provide such functionality through the LUA API
> hook to override backend server dynamically from Lua end
> if ts.client_request.header[Host] == 'example.test.com' then
> ts.client_request.set_url_host(
> test1.server.comts.client_request.set_url_port(80)elsif
> ts.client_request.header[Host] == 'example2.test.com' then
> ts.client_request.set_url_host( test2.server.com )
> ts.client_request.set_url_port(80)end
> Is it possible to do the same using Haproxy and Lua?
Hi,
Yes, you can, but the way is a little bit different.
You must create a new sample fetch in Lua. This fetch executes the
analysis of your HTTP resuest and returns the backend name. The
following code (untested) must set in a lua file:
core.register_fetches("choose_backend", function(txn)
if txn.sf.req_fhdr(Host) == 'example.test.com' then
return "backend1"
else if txn.sf.req_fhdr(Host) == 'other.domain.com' then
return "backend2"
[...]
end
return "default_backend"
end);
In the haproxy configuration file, you must load the lua file, and use
the new declared fetch inyour frontend:
global
[...]
lua-load your-lua-file.lua
[...]
frontend your_frt
[...]
use_backend %[lua.choose_backend]
[...]
Note that your example code seem to choose a backend regarding only the
header "host". In this case, the lua is useless, you can use the "maps"
to choose your backend. The configuration looks like this:
frontend yur_frt
[...]
use_backend %[req.fhdr(host),tolower,map(host_to_bck.map,default)]
[...]
The "default" argument is the name of the default backend. And the file
"host_to_bck.map" contains a mapping between the hostnames (in lower
case) and the backend names:
example.test.com backend1
other.domain.com backend2
etc...
Thierry
>
> Thank you,
> Mrunmayi