使用Mosquitto配置Nginx反向代理

z2acfund  于 2023-03-17  发布在  Nginx
关注(0)|答案(1)|浏览(507)

你好我来自这个职位Configure Nginx reverse proxy for MQTT
我有一个虚拟机,在此虚拟机中,我有nginx代理侦听客户端连接的端口1885,我希望它在所有Mosquitto容器(在本示例中,我删除了其他两个容器)之间重新分发消息,这将创建一个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;
        }
    }
}

我的问题是尽管能够suscribe到端口1885,但我不能从其他终端写入消息或使用mqtt资源管理器连接
运行docker-composite 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 mosquito,这个图像应该有端口1883,尽管后来改变了它的访问权限,如果是这样的话,没有问题,但我不知道还有什么是访问它使冲突。
你能帮帮我吗?

chhqkbe1

chhqkbe11#

您正在尝试的一些问题
1.您已将mosquitto.conf装载到错误的路径。配置文件应装载到/mosquitto/config/mosquitto.conf而不是/etc/mosquitto/mosquitto.conf。有关https://hub.docker.com/_/eclipse-mosquitto的详细信息,请参阅Docker集线器上的条目

  1. environment部分没有执行任何有用的操作,这些值需要包含在mosquitto.conf
    1.你不能使用原生MQTT的HTTP代理,因为有完全不同的协议。你可以配置Nginx在流代理模式下工作。你可以配置mosquito来支持WebSockets上的MQTT(同样你需要提供一个配置文件来启用它),这将与HTTP代理一起工作,但是依赖于你所有的客户端也支持WebSockets上的MQTT,并且被专门配置来使用它。
    1.除非您在多个MQTT代理之间配置桥接,否则此设置将无法工作。因为如果没有桥接,发布到一个代理的消息将不会最终传递到连接到其他两个代理的任何客户端。(moffesto容器之间的共享卷将不会共享任何消息)

相关问题