If you are building a system that serves both HTTP and MQTT, I sure that you have to use HAProxy. So in this post, I will share you the way to setup HAProxy to serve Nginx and MQTT Broker.
Your haproxy.cfg file should contain the source code below:
Haproxy MQTT Configuration
1 2 3 4 5 6 |
listen mqtt bind *:1883 mode tcp option tcplog balance leastconn server broker_1 broker:1883 check |
Note: broker is MQTT Broker’s hostname
Haproxy Nginx
1 2 3 4 5 6 |
listen http bind *:80 mode tcp option tcplog balance leastconn server nginx_1 nginx:80 check |
For monitoring Haproxy, You can add Haproxy Stats Configuration
1 2 3 4 5 6 |
listen stats :1936 mode http stats enable stats hide-version stats realm Haproxy\ Statistics stats uri / |
Final Result haproxy.cfg
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
global maxconn 99999 maxpipes 99999 tune.maxaccept 500 log 127.0.0.1 local0 log 127.0.0.1 local1 notice defaults log global mode http option dontlognull timeout connect 5000ms timeout client 50000ms timeout server 50000ms listen http bind *:80 mode tcp option tcplog balance leastconn server nginx_1 nginx:80 check listen mqtt bind *:1883 mode tcp option tcplog balance leastconn server broker_1 broker:1883 check listen stats :1936 mode http stats enable stats hide-version stats realm Haproxy\ Statistics stats uri / |