How to made Websocket traffic to go via HAProxy
The solution was simple: Tell HAProxy to tag Websocket traffic, and set up a separate but otherwise identical backend for this specific use case.
One of typical applications which is using Websocket traffic is Slack. An excelent opensource alternative of it named Mattermost. The result looks something like this in the config file named haproxy.conf:
acl host_ws hdr_beg(Host) -i ws.
acl hdr_connection_upgrade hdr(Connection) -i upgrade
acl hdr_upgrade_websocket hdr(Upgrade) -i websocket
use_backend bk_ws_mattermost if host_ws { hdr(Host) -i slack.ostreff.info }
use_backend bk_ws_mattermost if hdr_connection_upgrade hdr_upgrade_websocket { hdr(Host) -i slack.ostreff.info }
use_backend bk_mattermost if { hdr(Host) -i slack.ostreff.info }
backend bk_mattermost
server mattermost 127.0.0.1:8065 check
backend bk_ws_mattermost
server mattermost 127.0.0.1:8065 check
Here is a good article how to install Mattermost server on FreeBSD inside a jail.