【问题标题】:Unexpected HTTP Request: POST /mqtt/auth意外的 HTTP 请求:POST /mqtt/auth
【发布时间】:2019-01-22 04:38:20
【问题描述】:

我是 emqtt 的新手。我正在尝试使用 emq_auth_http 但它不起作用。

我有这 3 个请求来控制一些数据并将状态为 200 的数据发回。

app.post('/mqtt/auth', function(req, res) {
console.log('This is body ', req.body);
res.status(200).send(req.body);
});
app.post('/mqtt/superuser', function(req, res) {
console.log('This is body in superuser ', req.body);
res.status(200).send(req.body);
});
app.get('/mqtt/acl', function(req, res) {
console.log('This is params in acl ', req.params);
res.status(200).send(req.body);
});

邮递员的请求工作正常。

我已经用 docker 在 windows 上配置了我的 emqtt。我已将配置文件放在 /etc/plugins/emq_auth_http.conf 中。

这是我的配置文件

## Variables: %u = username, %c = clientid, %a = ipaddress, %P = password, %t = topic

auth.http.auth_req = http://127.0.0.1:3000/mqtt/auth
auth.http.auth_req.method = post
auth.http.auth_req.params = clientid=%c,username=%u,password=%P

auth.http.super_req = http://127.0.0.1:3000/mqtt/superuser
auth.http.super_req.method = post
auth.http.super_req.params = clientid=%c,username=%u

## 'access' parameter: sub = 1, pub = 2
auth.http.acl_req = http://127.0.0.1:3000/mqtt/acl
auth.http.acl_req.method = get
auth.http.acl_req.params = 
access=%A,username=%u,clientid=%c,ipaddr=%a,topic=%t

然后我从仪表板启用了 emq_auth_http

现在,当我尝试将我的 mqtt 客户端连接到我的服务器时,它没有调用 api。它记录

09:28:29.642 [error] Unexpected HTTP Request: POST /mqtt/auth
09:28:29.644 [error] Client(19645050-9d1b-4c50-acf9- 
c1fe7e69eea8@172.17.0.1:60968): Username 'username' login failed for 404

有什么我错过的吗?为什么它不起作用?

谢谢

【问题讨论】:

  • node.js 代码在哪里运行?鉴于 emq 在 docker 中运行,您确定 127.0.0.1 指向您认为的位置吗? node.js 代码在被 emq 调用时也会记录什么吗?
  • Node js 代码正在我的本地主机上运行。我还通过在服务器上部署我的代码来测试它。这就是 nodejs 代码不记录任何内容的问题。我认为请求没有到达 nodejs 服务器

标签: mqtt emq


【解决方案1】:

容器中的 127.0.0.1 是指容器本身,而不是主机。你应该设置主机ip,你可以通过发出命令/sbin/ip route|awk '/default/ { print $3 }'从容器中获取主机ip,可以找到here

ps:这样你可以获取docker机器的ip而不是主机,如果你的服务是由windows提供的,你可以从容器10.0.75.1获取主机的ip。你可以在 How to connect to docker host from container on Windows 10 (Docker for Windows)

【讨论】:

  • 解释为什么这是一个答案。
  • 在配置文件中ip设置为127.0.0.1,但是在容器中这个ip指的是容器本身,配置文件中正确的ip地址必须是主机ip,使用命令上面在容器中会给主机 ip,在大多数情况下,对于 windows 的 docker 是 10.0.75.1。确保服务允许来自主机外部的连接。就该服务而言,您的 docker 容器是另一台机器。还要确保 Windows 防火墙允许与服务进行通信
  • 简单地说,在您的网络中使用您的内部 IP 而不是指本地主机。因为容器内的 localhost 与你实际机器上的 localhost 不同。
猜你喜欢
  • 2018-05-18
  • 1970-01-01
  • 2022-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-05
相关资源
最近更新 更多