使用 Mosquitto 配置 Nginx 反向代理

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

你好我来自这篇文章为MQTT配置Nginx反向代理

我有一个虚拟机,在这个虚拟机中我有 nginx 代理侦听客户端连接的端口 1885,我想让它在所有 Mosquitto 容器之间重新分发消息(在这个例子中我删除了其他两个容器我有)这个来创建一个蚊子体积

version: '3'
services:
  nginx:
    image: nginx:latest
    ports:
      - "1885:1885"
      - "443:443"
    volumes:
      - ./nginx.conf:/var/lib/docker/volumes/nginx-conf/_data/nginx.conf
    networks:
      - backend

  mosquitto1:
    image: eclipse-mosquitto
    ports:
      - "1886:1886"
    environment:
      - listener=1886:1886
      - allow_anonymous=true
    networks:
      - backend
    volumes:
      - ./mosquitto.conf:/etc/mosquitto/mosquitto.conf

networks:
  backend:
    driver: bridge

然后我有 nginx.conf

events {
}

http {
    upstream mosquitto_backend {
        
    zone tcp_mem 64k;
    proxy_protocol on;
    }

    server {
        listen 1885;
        server_name localhost;
    log_format mqtt '$remote_addr [$time_local] $protocol $status $bytes_sent $bytes_received $session_time "$request" "$http_referer" "$http_user_agent" "$upstream_addr"';
    access_log /var/log/nginx/mqtt_access.log mqtt;
    error_log  /var/log/nginx/mqtt_error.log; # Health check notifications
        location / {
            proxy_pass http://mosquitto_backend;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
        }
    }
}

我遇到的问题是尽管能够订阅端口 1885,但我无法从其他终端写入消息或使用 mqtt explorer 连接

运行 docker-compose up 将在它尝试使用端口 1883 并与某些东西发生冲突时返回此消息

docker-compose up --remove-orphans
[+] Running 2/2
 ⠿ Container data-mosquitto1-1  Created                                                                                                                          0.1s
 ⠿ Container data-nginx-1       Created                                                                                                                          0.0s
Attaching to data-mosquitto1-1, data-nginx-1
data-mosquitto1-1  | 1678977906: mosquitto version 2.0.15 starting
data-mosquitto1-1  | 1678977906: Config loaded from /mosquitto/config/mosquitto.conf.
data-mosquitto1-1  | 1678977906: Starting in local only mode. Connections will only be possible from clients running on this machine.
data-mosquitto1-1  | 1678977906: Create a configuration file which defines a listener to allow remote access.
data-mosquitto1-1  | 1678977906: For more details see https://mosquitto.org/documentation/authentication-methods/
data-mosquitto1-1  | 1678977906: Opening ipv4 listen socket on port 1883.
data-mosquitto1-1  | 1678977906: Opening ipv6 listen socket on port 1883.
data-mosquitto1-1  | 1678977906: Error: Address not available
data-mosquitto1-1  | 1678977906: mosquitto version 2.0.15 running
data-nginx-1       | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
data-nginx-1       | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
data-nginx-1       | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
data-nginx-1       | 10-listen-on-ipv6-by-default.sh: info: IPv6 listen already enabled
data-nginx-1       | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
data-nginx-1       | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
data-nginx-1       | /docker-entrypoint.sh: Configuration complete; ready for start up
data-nginx-1       | 2023/03/16 14:45:06 [notice] 1#1: using the "epoll" event method
data-nginx-1       | 2023/03/16 14:45:06 [notice] 1#1: nginx/1.23.3
data-nginx-1       | 2023/03/16 14:45:06 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6) 
data-nginx-1       | 2023/03/16 14:45:06 [notice] 1#1: OS: Linux 3.10.0-1160.83.1.el7.x86_64
data-nginx-1       | 2023/03/16 14:45:06 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
data-nginx-1       | 2023/03/16 14:45:06 [notice] 1#1: start worker processes
data-nginx-1       | 2023/03/16 14:45:06 [notice] 1#1: start worker process 22

我用lsof -i:1883和netstat -tuln看这个端口有没有被占用

我知道我正在使用一个图像 eclipse-mosquitto 并且该图像应具有端口 1883,尽管稍后更改它访问它并且如果是这样的话没问题,但我不知道还有什么正在访问它制作冲突。

你能帮帮我吗?

nginx docker-compose mqtt nginx-reverse-proxy mosquitto
1个回答
3
投票

你正在尝试的一些问题

  1. 您已将

    mosquitto.conf
    安装在错误的路径上。配置文件应该挂载在
    /mosquitto/config/mosquitto.conf
    而不是
    /etc/mosquitto/mosquitto.conf
    。有关详细信息,请参阅 Docker hub 上的条目https://hub.docker.com/_/eclipse-mosquitto

  2. environment
    部分没有做任何有用的事情,这些值需要包含在
    mosquitto.conf

  3. 您不能将 HTTP 代理与本机 MQTT 一起使用,它们只是完全不同的协议。您可以在流代理模式下配置 Nginx,这将起作用。您可以配置 mosquitto 以支持 WebSockets 上的 MQTT(同样您需要提供一个配置文件来启用此功能),它将与 HTTP 代理一起使用,但确实依赖于您的所有客户端也支持 WebSockets 上的 MQTT 并专门配置为使用它.

  4. 除非您配置多个 MQTT 代理之间的桥接,否则此设置将不起作用。由于没有桥接,发布到一个代理的消息将不会最终传递给连接到其他 2 个代理的任何客户端。 (mosquitto 容器之间的共享卷不会共享任何消息)

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