Greetings,

On 06/29/2017 05:36 PM, Burak Çayır wrote:
Hello,

I am a CS student and I am trying to learn HAproxy and Lua API.

I want to load web server pages faster than before. Is it possible with HAproxy Lua API ? If it is possible , which algorithm I should use ?

The correct algorithm depends on the problem your trying to solve. If your just trying to route via host header I'd not recommend using LUA, instead see my method below. If you are trying to learn the LUA API I'd pick a more complicated task that can't be done directly in HAProxy (verifying and routing based on an authentication header, perhaps).

my getbackend.cfg
global
|daemon log /dev/log local0 log /dev/log local1 notice maxconn 50000 lua-load /etc/haproxy/getbackend.lua defaults log global retries 3 backlog 10000 maxconn 10000 timeout connect 3s timeout client 30s timeout server 30s timeout tunnel 3600s timeout http-keep-alive 1s timeout http-request 15s timeout queue 30s timeout tarpit 60s option redispatch option http-server-close option dontlognull frontend mywebserver bind *:8080 mode http use_backend %[lua.choose_backend] backend backend1 balance roundrobin mode http server ws1 192.168.122.232:8080 <http://192.168.122.232:8080/> server ws3 192.168.122.219:8080 <http://192.168.122.219:8080/> backend backend2 balance roundrobin mode http server ws2 192.168.122.72:8080 <http://192.168.122.72:8080/> server ws4 192.168.122.172:8080 <http://192.168.122.172:8080/>|
​and my getbackend.lua

​core.register_fetches("choose_backend", function(txn)
|if txn.sf:req_fhdr("host") == 'test.com:8080 <http://test.com:8080/>' then return "backend1" elseif txn.sf:req_fhdr("host") == 'example.com:8080 <http://example.com:8080/>' then return "backend2" end end)|
You could select a backend based on the host header in LUA, but that will be substantially slower than just doing it directly in the HAProxy configuration:
   use_backend backend1 if { hdr(Host) -i test.com }
   use_backend backend2 if { hdr(Host) -i example.com }

That also has the advantage of being case-insensitive. If you wanted to verify the port is 8080 add '{ dst_port 8080 }' to the end of that, the port isn't part of the host header fetch (likely the reason why your LUA script isn't doing what you expect).

Thanks,
- Chad
​King Regards,
​Burak​
​

--
*Burak Çayır*
/
/
/539 578 3671
/
/burakcayir.net <http://burakcayir.net>
/
/Ankara - Türkiye/

Reply via email to