# Load balancer for Wazuh agents: NGINX, HAProxy and the HAProxy helper

> How to spread Wazuh 4.14 agents across cluster workers: NGINX stream, HAProxy with leastconn, the HAProxy helper, Dataplane API and 19 haproxy_helper options.

Published: 2026-11-25. Category: Deployment. Tags: Wazuh 4. URL: https://wazuh.blog/en/blog/wazuh-agent-load-balancer/ Polish original: https://wazuh.blog/blog/wazuh-load-balancer-agentow/

Wazuh agents can be pointed at a single load balancer address instead of listing the cluster nodes in each of them. I describe the NGINX and HAProxy configuration from the 4.14 documentation, how the HAProxy helper built into the cluster works, and all the options of its `ossec.conf` section. The addresses in the examples are synthetic (192.0.2.0/24), and the passwords and user names are dummies.

## Two ways to connect agents to a cluster

The documentation describes two methods, both through the `<client>` block in the agent's `/var/ossec/etc/ossec.conf`. In failover mode the agent gets a list of several `<server>` blocks (master and workers) and, after a disconnect, moves cyclically to the next node on the list. In the second method the agent has one load balancer address, and the balancer picks the worker that gets the session.

Wazuh recommends a load balancer. The documentation names two advantages: a more even load distribution and the ability to report to newly added nodes without changing agent configuration. On the agent side the change comes down to one block:

```xml
<client>
  <server>
    <address>192.0.2.5</address>
    <port>1514</port>
    <protocol>tcp</protocol>
  </server>
</client>
```

After the edit, restart the agent (`systemctl restart wazuh-agent`). The default values of `port` (1514) and `protocol` (`tcp`) come from the `client` section reference. The reference also describes `max_retries` (default 5) and `retry_interval` (default 10 s) as parameters of the `<server>` block.

Agent registration (enrollment) goes through a separate port. The `manager_address` option in the `enrollment` block is described in the documentation like this: if it is not set, the agent enrolls with the same manager it was pointed to for the connection. With the balancer address in `<server>`, enrollment requests therefore go to the same address, on port 1515 (the default `port` in `enrollment`). The details of enrollment itself are in [agent enrollment](/en/blog/wazuh-agent-enrollment/).

## Ports and node roles

The balancer serves two TCP streams. Port 1515 is for agent registration, port 1514 for event reporting. Cluster node communication uses port 1516. Workers initiate the connection to the master themselves, and a cluster has exactly one master. The node architecture is described in the [post on Wazuh architecture](/en/blog/wazuh-architecture/), and cluster health checks in the [post on cluster operations](/en/blog/wazuh-cluster-operations/).

## NGINX: the stream module

The documentation example uses a `stream` block in `nginx.conf` (typical paths: `/usr/local/nginx/conf`, `/etc/nginx`, `/usr/local/etc/nginx`). Registration goes only to the master, reporting to the workers with the `hash $remote_addr consistent` directive:

```text
stream {
   upstream master {
       server 192.0.2.10:1515;
   }
   upstream mycluster {
       hash $remote_addr consistent;
       server 192.0.2.11:1514;
       server 192.0.2.12:1514;
   }
   server {
       listen 1515;
       proxy_pass master;
   }
   server {
       listen 1514;
       proxy_pass mycluster;
   }
}
```

After changing the configuration, the documentation tells you to check the syntax with `nginx -t` (the expected result is "syntax is ok" and "test is successful"), then reload the service: `nginx -s reload`.

The `hash $remote_addr consistent` directive routes connections by the client's source address. The Wazuh documentation does not comment on this decision. My reading of the configuration: behind NAT many agents share the same source address and land on the same worker. Check the distribution by the number of agents per node before you call it even.

## HAProxy: basic configuration

The documentation examples are based on Ubuntu and HAProxy 2.8 (an LTS version). The file `/etc/haproxy/haproxy.cfg` defines two frontend-backend pairs in `tcp` mode, each with the `balance leastconn` algorithm and a `check` on every server. In the documentation the `global` section also contains `chroot`, `pidfile`, `user`, `group` and `stats socket`. Below is a shortened version without them:

```text
global
          maxconn         4000
          log 127.0.0.1 local2 info

defaults
          mode http
          maxconn 4000
          log global
          option redispatch
          option dontlognull
          option tcplog
          timeout check 10s
          timeout connect 10s
          timeout client 1m
          timeout queue 1m
          timeout server 1m
          retries 3

frontend wazuh_register_front
          mode tcp
          bind :1515
          default_backend wazuh_register

backend wazuh_register
          mode tcp
          balance leastconn
          server master 192.0.2.10:1515 check
          server worker1 192.0.2.11:1515 check
          server worker2 192.0.2.12:1515 check

frontend wazuh_reporting_front
          mode tcp
          bind :1514 name wazuh_reporting_front_bind
          default_backend wazuh_reporting

backend wazuh_reporting
          mode tcp
          balance leastconn
          server master 192.0.2.10:1514 check
          server worker1 192.0.2.11:1514 check
          server worker2 192.0.2.12:1514 check
```

I checked this file locally with `haproxy -c -f haproxy.cfg` on HAProxy 3.4.8 (exit code 0). I changed one thing from the documentation: I renamed the `wazuh_register` frontend to `wazuh_register_front`. In the original the frontend and backend share a name, and HAProxy 3.4.8 rejected that with a message that such a name is "no longer supported as of 3.3". I did not check whether the original passes on the recommended 2.8 LTS, because I do not have it locally.

The two documentation examples differ on registration. NGINX sends port 1515 only to the master, while the `wazuh_register` backend in HAProxy lists the master and the workers. The documentation does not explain the difference.

The documentation also states that HAProxy supports, among others, round-robin, leastconn and source, and that when a manager node goes down, agents connect to another cluster node.

## HAProxy helper

The HAProxy helper is an optional mechanism that changes the HAProxy configuration on the fly depending on the state of the Wazuh cluster. It runs as a separate thread started together with the cluster daemon and can do three things:

- add and remove servers in the Wazuh backend (1514/tcp) when the cluster composition changes, for example when a new worker joins;
- spread surplus agents when a new server arrives in the backend;
- rebalance agents when the imbalance exceeds the configured tolerance.

The documentation gives two requirements: an HAProxy instance with the least connections algorithm (`leastconn`) and, recommended, version 2.8 LTS. The helper talks to HAProxy through the Dataplane API, so you have to configure that first.

### Dataplane API

The configuration lives in `/etc/haproxy/dataplaneapi.yml`. The HTTP variant looks like this:

```yaml
dataplaneapi:
   host: 0.0.0.0
   port: 5555
   transaction:
       transaction_dir: /tmp/haproxy
   user:
   -  insecure: true
      password: example-password
      name: example-user
haproxy:
   config_file: /etc/haproxy/haproxy.cfg
   haproxy_bin: /usr/sbin/haproxy
   reload:
      reload_delay: 5
      reload_cmd: service haproxy reload
      restart_cmd: service haproxy restart
```

The documentation recommends HTTPS. It requires the fields `scheme: [https]` and `tls` in this file, with the subfields `tls_port` (6443 in the example), `tls_certificate` and `tls_key`. The `tls_ca` field is needed only for client-side certificates. The certificate for the example is issued by the command `openssl req -x509 -newkey rsa:4096 ... -sha256 -nodes -addext "subjectAltName=DNS:<FQDN>"`.

You download the `dataplaneapi` binary in a version matching the installed HAProxy (from the haproxytech releases), copy it to `/usr/local/bin/` and start it with `dataplaneapi -f /etc/haproxy/dataplaneapi.yml &`. A request that returns JSON with the API version checks that the API works:

```bash
curl -X GET --user example-user:example-password http://localhost:5555/v2/info
```

For HTTPS the documentation uses `curl -k` and the address `https://localhost:6443/v2/info`.

### The haproxy_helper section in ossec.conf

According to the documentation, the configuration is added only on the master, inside the `<cluster>` block in `/var/ossec/etc/ossec.conf`. The HTTPS version:

```xml
<haproxy_helper>
   <haproxy_disabled>no</haproxy_disabled>
   <haproxy_address>192.0.2.5</haproxy_address>
   <haproxy_user>example-user</haproxy_user>
   <haproxy_password>example-password</haproxy_password>
   <haproxy_protocol>https</haproxy_protocol>
   <haproxy_port>6443</haproxy_port>
   <haproxy_cert>/etc/haproxy/ssl/example-haproxy.crt</haproxy_cert>
   <client_cert>/var/ossec/etc/example-client.crt</client_cert>
   <client_cert_key>/var/ossec/etc/example-client.key</client_cert_key>
</haproxy_helper>
```

The file in `haproxy_cert` should match the `tls_certificate` field from `dataplaneapi.yml`, and `client_cert` the `tls_ca` field. After saving, restart the manager (`systemctl restart wazuh-manager`) and check `/var/ossec/logs/cluster.log`. The documentation shows the expected entries `[HAPHelper] [Main] Proxy was initialized` and `Starting HAProxy Helper`, and between them a message that the helper waits 12 s to make sure only one HAProxy process exists.

### Change in haproxy.cfg

The only warning in the documentation reads like this: for the helper to work correctly, `haproxy.cfg` must not contain a frontend with port 1514. In the example file, the section with `wazuh_reporting_front` is preceded by the comment "Do not include the following if you will enable HAProxy Helper". The documentation does not expand on it. For the local syntax test I removed only the 1514 frontend and left the `wazuh_reporting` backend. The `haproxy -c` result was valid, but the documentation does not say whether the backend should stay.

## haproxy_helper options

The `cluster` section reference lists 19 options. The table below collects them with their default values.

| Option | Default | Allowed values and meaning |
|--------|---------|----------------------------|
| haproxy_disabled | yes | yes or no. With yes the helper does not start |
| haproxy_address | none | IP address or DNS name the helper uses to connect to HAProxy |
| haproxy_user | none | any user name |
| haproxy_password | none | any password |
| haproxy_port | 5555 | port from 1024 to 65535 |
| haproxy_protocol | http | http or https |
| haproxy_cert | none | path to the HAProxy certificate file |
| client_cert | none | path to the client-side certificate |
| client_cert_key | none | path to the client certificate key |
| client_cert_password | none | client certificate password |
| haproxy_backend | wazuh_reporting | name of the backend created in HAProxy |
| haproxy_resolver | none | name of the HAProxy resolver |
| excluded_nodes | none | cluster node names separated by commas, excluded from agent distribution |
| frequency | 60 | seconds to the next check, integer from 10 |
| agent_chunk_size | 300 | number of agents reconnected at once, from 100 |
| agent_reconnection_time | 5 | seconds between agent batches, from 0 |
| agent_reconnection_stability_time | 60 | seconds to wait after moving agents to a new worker, from 10 |
| imbalance_tolerance | 0.1 | threshold for treating the cluster as unbalanced, number from 0 to 1 |
| remove_disconnected_node_after | 240 | minutes until a disconnected worker is removed, from 0 |

The reference example sets `haproxy_backend` to `wazuh_cluster`, `agent_chunk_size` to 100 and `remove_disconnected_node_after` to 10, so it departs from the defaults. If you change `haproxy_backend`, the name must match the backend the helper is meant to serve in HAProxy. This is my reading of the option description, the documentation gives no separate rule.

## Checking on the agent side

Whether an agent actually went through the balancer shows in the state file `/var/ossec/var/run/wazuh-agentd.state` (Linux). The `status` value is `pending`, `disconnected` or `connected`, and `disconnected` means no acknowledgment for the last 60 s or a lost connection. The details of states and disconnect thresholds are in the [post on disconnected agents](/en/blog/wazuh-agent-disconnections/). The connection on port 1514/TCP is checked with `netstat -vatunp|grep wazuh-agentd` on the agent, and the list of agents and statuses on the server side comes from `/var/ossec/bin/agent_control -l`.

The `force_reconnect_interval` parameter in the `client` section (default `0s`, meaning disabled) forces the agent to reconnect even when two-way connectivity is healthy. The documentation lists it as the only setting of this section that goes into the shared configuration.

## What the documentation does not specify

- Agent behavior when the load balancer itself stops working or is restarted. The documentation describes a manager node failure, but it does not describe balancer redundancy or combining the balancer address with additional `<server>` blocks.
- The mechanism the helper uses to force agents to move, and whether it covers enrollment connections on port 1515.
- Permissions of the Dataplane API user and compatibility with HAProxy 3.x. The recommended version is 2.8 LTS.
- Whether the `haproxy_helper` configuration should be absent on workers. The documentation steps concern only the master, and helper behavior with an unavailable master is not described.

## Sources

- https://documentation.wazuh.com/4.14/user-manual/wazuh-server-cluster/load-balancers.html
- https://documentation.wazuh.com/4.14/user-manual/wazuh-server-cluster/agent-connections.html
- https://documentation.wazuh.com/4.14/user-manual/reference/ossec-conf/cluster.html
- https://documentation.wazuh.com/4.14/user-manual/reference/ossec-conf/client.html
- https://documentation.wazuh.com/4.14/user-manual/wazuh-server-cluster/cluster-nodes-configuration.html
- https://documentation.wazuh.com/4.14/user-manual/wazuh-server-cluster/how-server-cluster-works.html
- https://documentation.wazuh.com/4.14/user-manual/agent/agent-management/agent-connection.html
