本来mqtt用的好好的,公司信息安全把端口给封了,就给一个80,所以mqtt只能走这个端口,记录一下过程
1 生成密码文件 搞一个pwd.txt 写着username: pwd 然后 mosquitto_passwd -U passwordfile 就会生成一个pwd.txt
2 mosquitto的配置,注意要走websocket
bash
persistence true
persistence_location /mosquitto/data
log_dest file /mosquitto/log/mosquitto.log
# 服务绑定的端口号 默认是1883
# 服务绑定的IP地址 websockets 一定要在listener的下方,不然会错误
listener 18231 0.0.0.0
protocol websockets
allow_anonymous false
password_file /etc/mosquitto/pwd.txt
3 docker 启动命令,注意文件的映射:
bash
docker run -d --name mqtt --net host --restart always -v /data/mqtt/log:/mosquitto/log -v /data/mqtt/data:/mosquitto/data -v /data/mydocker/mqtt/mosquitto.conf:/mosquitto/config/mosquitto.conf -v /data/mydocker/mqtt/pwd.txt:/etc/mosquitto/pwd.txt eclipse-mosquitto:latest
4 nginx的配置,位置在server里面,注意/mqtt是默认的,有的是/ws,我这个是/mqtt
bash
location /mqtt {
proxy_pass "http://$myhost:18231";
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header Host $host;
}