端口 8883 配置
端口 8883 配置
本节介绍 MQTTS(MQTT over TLS)端口 8883 的配置。8883 是 IANA 分配的 MQTT over TLS 标准端口。学习完成后,您将能够:
- 配置 Mosquitto 监听 8883 端口
- 配置 ESP32 连接 8883 端口
- 配置防火墙允许 8883 端口
- 理解同时支持 1883 和 8883 的双端口策略
在开始本节之前,请确保:
- Mosquitto TLS 已基本配置
- 证书路径已正确配置
- 了解基本的防火墙概念
Standard MQTT Ports
Section titled “Standard MQTT Ports”| 端口 | 协议 | 加密 | 用途 |
|---|---|---|---|
| 1883 | MQTT | 明文 | 标准 MQTT(无加密) |
| 8883 | MQTTS | TLS 加密 | MQTT over TLS(推荐) |
| 8083 | MQTT+WS | 明文 | MQTT over WebSocket |
| 8084 | MQTT+WSS | TLS 加密 | MQTT over Secure WebSocket |
Mosquitto Dual Port Config
Section titled “Mosquitto Dual Port Config”同时支持明文和加密
Section titled “同时支持明文和加密”# 监听 1883(明文)—— 可选,用于内网旧设备listener 1883protocol mqtt
# 监听 8883(TLS 加密)—— 必选,用于生产环境listener 8883protocol mqtt
# 8883 端口的 TLS 配置cafile /mosquitto/certs/fullchain.pemcertfile /mosquitto/certs/fullchain.pemkeyfile /mosquitto/certs/privkey.pemtls_version tlsv1.2仅加密模式(生产推荐)
Section titled “仅加密模式(生产推荐)”# 仅监听 TLS 加密端口listener 8883protocol mqtt
# TLS 配置cafile /mosquitto/certs/fullchain.pemcertfile /mosquitto/certs/fullchain.pemkeyfile /mosquitto/certs/privkey.pemtls_version tlsv1.2
# 可选:将 1883 重定向到 8883# 但 Mosquitto 不支持自动重定向,需在客户端配置Docker Port Mapping
Section titled “Docker Port Mapping”端口映射配置
Section titled “端口映射配置”services: mosquitto: image: eclipse-mosquitto:2 ports: # 宿主机端口:容器端口 - "1883:1883" # MQTT 明文(可选,内网使用) - "8883:8883" # MQTTS 加密(生产环境必选) - "9001:9001" # WebSocket(可选)网络安全策略
Section titled “网络安全策略”# 如果需要限制访问来源ports: # 仅允许内网访问 1883 - "192.168.1.0/24:1883:1883" # 允许所有来源访问 8883 - "0.0.0.0:8883:8883"Firewall Configuration
Section titled “Firewall Configuration”UFW(Ubuntu)
Section titled “UFW(Ubuntu)”# 允许 8883 端口(MQTTS 加密)sudo ufw allow 8883/tcp comment 'MQTT over TLS'
# 可选:内网场景允许 1883sudo ufw allow from 192.168.1.0/24 to any port 1883 proto tcp comment 'MQTT internal'
# 查看防火墙状态sudo ufw status verbose
# 输出示例:# To Action From# -- ------ ----# 8883/tcp ALLOW Anywhere# 1883/tcp ALLOW 192.168.1.0/24iptables
Section titled “iptables”# 允许 8883 端口sudo iptables -A INPUT -p tcp --dport 8883 -j ACCEPT
# 限制 1883 端口仅内网访问sudo iptables -A INPUT -p tcp --dport 1883 -s 192.168.1.0/24 -j ACCEPTsudo iptables -A INPUT -p tcp --dport 1883 -j DROP
# 保存规则sudo apt install iptables-persistentsudo netfilter-persistent saveESP32 Port Configuration
Section titled “ESP32 Port Configuration”#include <WiFi.h>#include <WiFiClientSecure.h>#include <PubSubClient.h>
// TLS 配置const char* mqtt_server = "mqtt.example.com"; // DynDNS 域名const int mqtt_port = 8883; // MQTTS 端口const char* mqtt_user = "esp32_device";const char* mqtt_password = "device_password";
// 非 TLS 配置(旧方案,1883 端口)// const int mqtt_port = 1883;
WiFiClientSecure espClient;PubSubClient client(espClient);
void setup() { Serial.begin(115200); WiFi.begin(ssid, password);
// 等待 WiFi 连接 while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.println("连接 WiFi..."); }
// 设置 TLS 证书(不验证或使用 CA 证书) espClient.setInsecure(); // 开发测试用,不验证证书
// MQTT 配置 client.setServer(mqtt_server, mqtt_port); client.setCallback(callback);}
void reconnect() { while (!client.connected()) { Serial.print("连接到 MQTT Broker..."); if (client.connect("ESP32_Device", mqtt_user, mqtt_password)) { Serial.println("已连接 (TLS 加密)"); client.subscribe("esp32/control"); } else { Serial.print("连接失败, rc="); Serial.print(client.state()); Serial.println(" 5 秒后重试"); delay(5000); } }}Port Testing
Section titled “Port Testing”端口可达性测试
Section titled “端口可达性测试”# 1. 测试本地端口是否监听docker exec mosquitto netstat -tlnp | grep 8883# tcp 0 0 0.0.0.0:8883 0.0.0.0:* LISTEN
# 2. 测试远程端口是否可达nc -zv mqtt.example.com 8883# Connection to mqtt.example.com (203.0.113.45) port 8883 [tcp/*] succeeded!
# 3. 测试 TLS 握手openssl s_client -connect mqtt.example.com:8883 -servername mqtt.example.comNode-RED Broker 配置
Section titled “Node-RED Broker 配置”// Node-RED MQTT Broker 配置(TLS 版本){ "broker": "mqtt.example.com", "port": "8883", "tls": true, "usetls": true, "tlsconfig": { "ca": "", // 留空即可(Node-RED 信任系统 CA) "cert": "", "key": "", "servername": "mqtt.example.com", "tlsVersion": "tlsv1.2" }}Migration Strategy
Section titled “Migration Strategy”从 1883 迁移到 8883
Section titled “从 1883 迁移到 8883”迁移阶段:
阶段 1: 双端口运行 全部设备使用 1883(明文) 新设备开始使用 8883(加密)
阶段 2: 新设备默认 8883 新设备默认使用 8883 旧设备继续使用 1883 🔄 评估阶段
阶段 3: 旧设备分批迁移 每个设备 OTA 更新固件 将端口从 1883 改为 8883 ✅ 完成后停用 1883
阶段 4: 仅 8883 模式 关闭 1883 端口 所有设备使用加密连接 🔒 安全模式端口常见问题
Section titled “端口常见问题”| 问题 | 原因 | 解决方案 |
|---|---|---|
| ”Connection refused” 8883 | Mosquitto 未监听 8883 | 检查 mosquitto.conf listener 配置 |
| ”Connection timed out” | 防火墙阻止 8883 | 开启防火墙端口 8883/tcp |
| ”Certificate name mismatch” | 域名和证书不匹配 | 确认连接域名与证书 CN 一致 |
| ”Port 8883 already in use” | 端口被其他程序占用 | 停止占用进程或更换端口 |
| ”cannot assign requested address” | Docker 端口冲突 | 检查 docker-compose ports 配置 |
Pre-sales Key Points
Section titled “Pre-sales Key Points”端口策略建议
Section titled “端口策略建议”| 场景 | 端口配置 | 理由 |
|---|---|---|
| 开发环境 | 1883 + 8883 | 双端口便于调试 |
| 局域网生产 | 1883 (内网) + 8883 | 旧设备兼容 |
| 互联网生产 | 仅 8883 | 全部加密 |
| 高安全环境 | 仅 8883 + 证书验证 | 最高安全 |
Summary
Section titled “Summary”本节介绍了 8883 端口配置:
- 标准端口:1883(明文)、8883(加密)、8083/8084(WebSocket)
- 双端口策略:同时运行 1883 和 8883 实现平滑迁移
- 防火墙配置:开放 8883/tcp,限制 1883 内网访问
- ESP32 配置:修改端口为 8883,使用 WiFiClientSecure
- 迁移策略:双端口 → 新设备 8883 → 旧设备分批迁移 → 仅 8883