【问题标题】:MQTT doesn't work with global ipMQTT 不适用于全局 IP
【发布时间】:2018-04-17 10:41:33
【问题描述】:

一般信息:

  • IP:1.2.3.4
  • 服务器:Raspian Stretch (Debian 9)
  • 在服务器上运行 apache2 和 2 个网站。
  • 蚊子用户:user
  • moquittto 密码:psw

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

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

【问题讨论】:

  • 顶级 conf 文件 (/etc/mosquitto/mosquitto.conf) 中有什么。托管环境(例如 AWS)中的服务器也是如此,如果是的话,您是否检查过没有环境级别的防火墙,您也需要在其中打开端口。
  • @hardillb 我已经编辑了您需要的信息。服务器自托管在我家的树莓派 wifi 连接中。

标签: linux debian port mqtt


【解决方案1】:

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

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


如果您在消费者 NAT 路由器后面,并且使用路由器的外部 IP 地址,那么可能会出现一些问题:

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

【讨论】:

  • 谢谢,第2点解决了,忘记设置端口转发了
猜你喜欢
  • 2022-07-11
  • 2018-08-06
  • 1970-01-01
  • 2017-01-04
  • 2013-04-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-06
相关资源
最近更新 更多