MQTT 不适用于全局 IP



一般信息:

  • 知识产权:1.2.3.4
  • 服务器: Raspian Stretch (Debian 9)
  • 在服务器上运行带有 2 个网站的 apache2。
  • 莫斯基托用户:user
  • 莫奎特托密码:psw

我已经在我的服务器上安装了mosquitto并设置了用户和密码。 当我在本地主机中使用它时,我没有问题:

mosquitto_pub -h localhost -p 1883 -t topic -u "user" -P "psw" -m "new message"
mosquitto_sub -h localhost -p 1883 -t topic -u "user" -P "psw"

我收到new message没有任何问题

不幸的是,如果我使用我的公共 ip,它不再起作用:

mosquitto_pub -h 1.2.3.4 -p 1883 -t topic -u "user" -P "psw" -m "new message"
mosquitto_sub -h 1.2.3.4 -p 1883 -t topic -u "user" -P "psw"

几秒钟后,订阅者和发布者都会打印此错误:Error: Connection timed out

我可以想象是一个封闭的端口,但我不知道如何解锁它。

我试过这个,但没有改变任何东西:sudo iptables -A INPUT -p tcp -m tcp --dport 1883 -j ACCEPT

这些是我的配置文件:

/etc/mosquitto/conf.d/default.conf

allow_anonymous false
password_file /etc/mosquitto/passwd
listener 1883

/etc/mosquitto/mosquitto.conf

# Place your local configuration in /etc/mosquitto/conf.d/
#
# A full description of the configuration file is at
# /usr/share/doc/mosquitto/examples/mosquitto.conf.example
pid_file /var/run/mosquitto.pid
persistence true
persistence_location /var/lib/mosquitto/
log_dest file /var/log/mosquitto/mosquitto.log
include_dir /etc/mosquitto/conf.d

我认为您使用的 IP 实际上并不1.2.3.4- 如果您不拥有它并且它没有被列为私有/内部 IP,请不要在内部使用它。

同样,如果您不拥有它,请不要尝试在外部/互联网上使用它。


如果您位于消费者 NAT 路由器后面,并且使用路由器的外部 IP 地址,则存在几个潜在问题:

  1. 大多数消费者路由器不支持发夹 NAT,即:您无法使用路由器后面的外部 IP 访问服务(即使您已正确设置端口转发)。
    • 要测试这一点,请尝试从朋友家或通过其他互联网连接(例如:您的手机)进行连接。
    • 解决方案是直接连接到内部服务器。如果幸运的话,可以将路由器配置为处理发夹,或者您可以升级路由器。
  2. 您可能需要在路由器上设置端口转发,以便端口1883上的传入连接在内部转发到服务器。

最新更新