配置nginx重定向连接到MQTT Broker

fdx2calv  于 2023-04-05  发布在  Nginx
关注(0)|答案(1)|浏览(154)

I have an Issue to Acces MQTT mosquito Broker over an Nginx Container This is My docker撰写:

`version: '3.8'
services:
  mqtt-broker:
    image: eclipse-mosquitto:1.6.13
    container_name: mqtt-broker
    user: "1000:1000"
    ports:
      - 1883:1883
      - 9001:9001
    volumes:
      - ./config/mosquitto/mosquitto.conf:/mosquitto/config/mosquitto.conf.:ro

  mqtt-nginx:
    image: nginx:latest
    container_name: mqtt-nginx
    ports:
      - '1884:1884'
    depends_on:
      - mqtt-broker
    volumes:
      - ./config/nginx/nginx.conf:/etc/nginx/nginx.conf:ro`

这是我的nginx.conf:

events {
  worker_connections 1024;
}
stream {
  server {
    listen 1884;
      proxy_pass 127.0.0.1:1883;
        }
}

但是当我测试连接时:mosquitto_sub -h 127.0.0.1 -p 1884 -t mytopic我得到一个网络协议错误:Error: A network protocol error occurred when communicating with the broker.
如何修复我的配置!!

gt0wga4j

gt0wga4j1#

固定nginx配置:

events {
  worker_connections 1024;
}

stream {
  upstream mqtt_broker {
    server mqtt-broker:1883;
  }

  server {
    listen 1884;
    proxy_pass mqtt_broker;
  }
}

在我的docker-compose中添加网络:

version: '3.8'
services:
  mqtt-broker:
    image: eclipse-mosquitto:1.6.13
    container_name: mqtt-broker
    user: "1000:1000"
    ports:
      - 1883:1883
    networks:
      mqtt-net:

  volumes:
      - ./config/nginx/nginx.conf:/etc/nginx/nginx.conf:ro
    networks:
      mqtt-net:

networks:
  mqtt-net:

相关问题