I’m trying to setting up a reverse proxy that resolve localhost:8081 to a broker installed on an other machine.
My Nginx config file is:
worker_processes 1;
events {
worker_connections 1024;
}
server {
listen 8081;
server_name localhost;
location / {
proxy_pass tcp://192.168.1.177:1883;
}
}
But when I try to connect to the broker (from the machine where I’m configuring Nginx) with the command
mosquitto_sub -h localhost -p 8081 -t "stat/tasmota_8231A8/POWER1"
I get the error Connection refused.
Edit:
Mosquitto broker config:
persistence true
persistence_location /var/lib/mosquitto/
include_dir /etc/mosquitto/conf.d
listener 1883
allow_anonymous true
Edit
I try with this config file for nginx
worker_processes 1;
events {
worker_connections 1024;
}
stream {
listen 8081;
proxy_pass 192.168.1.77:1883;
}
3
Answers
This won’t work for native MQTT.
What you have configured is a HTTP proxy, but MQTT != HTTP.
You need to configure nginx as a stream proxy. e.g.
https://docs.nginx.com/nginx/admin-guide/tcp-udp-load-balancer/
Or configure mosquitto to support MQTT over WebSockets (Assuming the client supports this as well). Then you can use HTTP based proxying as WebSockets bootstrap via HTTP.
Connection refused is indicative of trying to connect to a port on which no service is listening. Please verify that nginx service is running and that a listener indeed exists. You can do this by
This command will show you ports that are taken. The configured port of the nginx listener should appear in the list
I got your scenario fully working with the following config (firewall turned off, SELinux set to permissive):
nginx config (on loadbalancer1):
}
mosquitto config on mqtt-node-1 and mqtt-node-2: