简体   繁体   中英

HaProxy configuration to balance Node Js - WebSockets

I have just installed HaProxy 1.6 on my Debian system and I would like to use it to distribute WebSocket (WS://) connections. The system is running 5 server instances of the same WebSocket service; these instances have the same IP Address but different ports:

192.168.10.1:801
192.168.10.1:802
192.168.10.1:803
192.168.10.1:804
192.168.10.1:805

I am looking for some advice to create a simple config file to listen requests on port 800 and distribute equally (maybe?) between servers.

defaults
    mode http
    # Set timeouts to your needs
    timeout client  5s
    timeout connect 5s
    timeout server  5s

frontend all 0.0.0.0:800
    mode http
    timeout client 120s

    option forwardfor
    # Fake connection:close, required in this setup.
    option http-server-close
    option http-pretend-keepalive

    acl is_sockjs path_beg /echo /broadcast /close
    acl is_stats  path_beg /stats

    use_backend sockjs if is_sockjs
    use_backend stats if is_stats
    default_backend static


backend sockjs
    # Load-balance according to hash created from first two
    # directories in url path. For example requests going to /1/
    # should be handled by single server (assuming resource prefix is
    # one-level deep, like "/echo").
    balance uri depth 2
    timeout server  120s
    server srv_sockjs1 127.0.0.1:12345
    # server srv_sockjs2 127.0.0.1:9998

backend static
    balance roundrobin
    server srv_static 127.0.0.1:801

backend stats
    stats uri /stats
    stats enable

I managed to make it work somehow by using the config file above that I have downloaded.. but it looks too complicated to me and is only accepting one connection at a time. If a second client connects, HA proxy disconnect the previous/first, and connects a new/second ..

Simplest way is to define one backend for all your websocket machines, and round-robin around them like so:

frontend ft_web
  bind 192.168.10.1:800 name http
  maxconn 10000
  default_backend bk_web

backend bk_web                      
  balance roundrobin
  server websrv1 192.168.10.1:801 maxconn 10000 weight 10 cookie websrv1 check
  server websrv2 192.168.10.1:802 maxconn 10000 weight 10 cookie websrv2 check
...

If it does not help, it seems that your timeouts are too short, try to define them all like this: (under defaults)

 timeout client          25s
  timeout connect          5s
  timeout server          25s
# timeout tunnel available in ALOHA 5.5 or HAProxy 1.5-dev10 and higher
  timeout tunnel        3600s
  timeout http-keep-alive  1s
  timeout http-request    15s
  timeout queue           30s
  timeout tarpit          60s

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM