为 MQTT 配置 Nginx 反向代理

问题描述 投票:0回答:2

我正在尝试设置一个反向代理,将 localhost:8081 解析为安装在另一台机器上的代理。 我的 Nginx 配置文件是:

worker_processes  1;

events {
    worker_connections 1024;
}

server {
    listen 8081;
    server_name localhost;

    location / {
        proxy_pass tcp://192.168.1.177:1883;
    }
}

但是当我尝试使用命令连接到代理(从我配置 Nginx 的机器)时

 mosquitto_sub -h localhost -p 8081 -t "stat/tasmota_8231A8/POWER1"

我收到错误连接被拒绝。

编辑: 蚊子经纪人配置:

persistence true
persistence_location /var/lib/mosquitto/

include_dir /etc/mosquitto/conf.d

listener 1883
allow_anonymous true

编辑 我尝试使用 nginx 的配置文件 worker_processes 1;

events {
    worker_connections 1024;
}
stream {
   listen 8081;
   proxy_pass 192.168.1.77:1883;
} 
nginx mqtt nginx-reverse-proxy nginx-config
2个回答
3
投票

这不适用于原生 MQTT。

你配置的是HTTP代理,但是MQTT != HTTP。

需要配置nginx作为流代理。例如

stream {
  server {
      listen 8081;
      proxy_pass 192.168.1.77:1883;
  }
}

https://docs.nginx.com/nginx/admin-guide/tcp-udp-load-balancer/

或者配置 mosquitto 以支持基于 WebSockets 的 MQTT(假设客户端也支持)。然后你可以通过 HTTP 使用基于 HTTP 的代理作为 WebSockets 引导程序。


0
投票

你确定nginx服务正在运行吗? Connection Refused 表示尝试连接到没有服务正在侦听的端口。请参阅以下屏幕截图作为示例。消息“这是没有证书的python客户端”只是收到的消息“

© www.soinside.com 2019 - 2024. All rights reserved.