跳转到内容

端口 8883 配置

端口 8883 配置

本节介绍 MQTTS(MQTT over TLS)端口 8883 的配置。8883 是 IANA 分配的 MQTT over TLS 标准端口。学习完成后,您将能够:

  • 配置 Mosquitto 监听 8883 端口
  • 配置 ESP32 连接 8883 端口
  • 配置防火墙允许 8883 端口
  • 理解同时支持 1883 和 8883 的双端口策略

在开始本节之前,请确保:

  • Mosquitto TLS 已基本配置
  • 证书路径已正确配置
  • 了解基本的防火墙概念
端口协议加密用途
1883MQTT明文标准 MQTT(无加密)
8883MQTTSTLS 加密MQTT over TLS(推荐)
8083MQTT+WS明文MQTT over WebSocket
8084MQTT+WSSTLS 加密MQTT over Secure WebSocket
/opt/mosquitto/config/mosquitto.conf
# 监听 1883(明文)—— 可选,用于内网旧设备
listener 1883
protocol mqtt
# 监听 8883(TLS 加密)—— 必选,用于生产环境
listener 8883
protocol mqtt
# 8883 端口的 TLS 配置
cafile /mosquitto/certs/fullchain.pem
certfile /mosquitto/certs/fullchain.pem
keyfile /mosquitto/certs/privkey.pem
tls_version tlsv1.2
# 仅监听 TLS 加密端口
listener 8883
protocol mqtt
# TLS 配置
cafile /mosquitto/certs/fullchain.pem
certfile /mosquitto/certs/fullchain.pem
keyfile /mosquitto/certs/privkey.pem
tls_version tlsv1.2
# 可选:将 1883 重定向到 8883
# 但 Mosquitto 不支持自动重定向,需在客户端配置
docker-compose.yml
services:
mosquitto:
image: eclipse-mosquitto:2
ports:
# 宿主机端口:容器端口
- "1883:1883" # MQTT 明文(可选,内网使用)
- "8883:8883" # MQTTS 加密(生产环境必选)
- "9001:9001" # WebSocket(可选)
# 如果需要限制访问来源
ports:
# 仅允许内网访问 1883
- "192.168.1.0/24:1883:1883"
# 允许所有来源访问 8883
- "0.0.0.0:8883:8883"
Terminal window
# 允许 8883 端口(MQTTS 加密)
sudo ufw allow 8883/tcp comment 'MQTT over TLS'
# 可选:内网场景允许 1883
sudo 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/24
Terminal window
# 允许 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 ACCEPT
sudo iptables -A INPUT -p tcp --dport 1883 -j DROP
# 保存规则
sudo apt install iptables-persistent
sudo netfilter-persistent save
#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);
}
}
}
Terminal window
# 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.com
// 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"
}
}
迁移阶段:
阶段 1: 双端口运行
全部设备使用 1883(明文)
新设备开始使用 8883(加密)
阶段 2: 新设备默认 8883
新设备默认使用 8883
旧设备继续使用 1883
🔄 评估阶段
阶段 3: 旧设备分批迁移
每个设备 OTA 更新固件
将端口从 1883 改为 8883
✅ 完成后停用 1883
阶段 4: 仅 8883 模式
关闭 1883 端口
所有设备使用加密连接
🔒 安全模式
问题原因解决方案
”Connection refused” 8883Mosquitto 未监听 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 配置
场景端口配置理由
开发环境1883 + 8883双端口便于调试
局域网生产1883 (内网) + 8883旧设备兼容
互联网生产仅 8883全部加密
高安全环境仅 8883 + 证书验证最高安全

本节介绍了 8883 端口配置:

  1. 标准端口:1883(明文)、8883(加密)、8083/8084(WebSocket)
  2. 双端口策略:同时运行 1883 和 8883 实现平滑迁移
  3. 防火墙配置:开放 8883/tcp,限制 1883 内网访问
  4. ESP32 配置:修改端口为 8883,使用 WiFiClientSecure
  5. 迁移策略:双端口 → 新设备 8883 → 旧设备分批迁移 → 仅 8883