在 IBM Bluemix 上使用 node-red 将 http 重定向到 https
redirect http to https using node-red on IBM Bluemix
在 Bluemix 上使用 node-red,我能够在 http://example.com
和 https://example.com
上获得我的应用程序 运行。但是,我希望我的应用程序受到保护并且只能通过 https 访问。我在 Bluemix 上的 node-red 运行 上找不到将 http 重定向到 https 的方法。我只找到了在节点上执行此操作的方法。js/express 和其他网络服务器。
我还尝试通过检测 msg.req.headers.$wssc
并使用 http 请求节点重定向到 https url 在我的应用程序上进行重定向,但它没有用(仍然得到 http 响应)。
在 DNS 上配置重定向也不是一个选项,因为据我所知,您不能将 http://example.com
重定向到 https://example.com
,但可以将 http://example.com
重定向到 https://www.example.com
。
请推荐。
您应该能够通过使用 msg.req.headers.x-forwarded-proto
值然后发送具有 301 状态的响应来执行此操作,例如
if (msg.req.headers['x-forwarded-proto'] == 'http') {
msg.statusCode = 301;
msg.headers = {
location: 'https://' + msg.req.get('host') + msg.req.originalUrl
}
msg.payload = "";
return [null, msg]
} else {
return [msg, null]
}
如果你把它放在一个有 2 个输出的函数节点中,它将把重定向发送到第二个输出,否则将它发送到第一个输出。
您也可以通过在 settings.js
中添加自己的 httpNodeMiddleware
函数来做到这一点,如下所示:
....
httpNodeMiddleware: function(req, res, next){
if (req.headers['x-forwarded-proto'] == 'http') {
res.redirect(301, 'https://' + req.get('host') + req.originalUrl);
} else {
next();
}
},
...
此方法意味着您不需要流程中的任何重定向逻辑
旁注:
您可能需要测试 msg.req.headers['$wssc']
而不是 msg.req.headers.$wssc
.
在 Bluemix 上使用 node-red,我能够在 http://example.com
和 https://example.com
上获得我的应用程序 运行。但是,我希望我的应用程序受到保护并且只能通过 https 访问。我在 Bluemix 上的 node-red 运行 上找不到将 http 重定向到 https 的方法。我只找到了在节点上执行此操作的方法。js/express 和其他网络服务器。
我还尝试通过检测 msg.req.headers.$wssc
并使用 http 请求节点重定向到 https url 在我的应用程序上进行重定向,但它没有用(仍然得到 http 响应)。
在 DNS 上配置重定向也不是一个选项,因为据我所知,您不能将 http://example.com
重定向到 https://example.com
,但可以将 http://example.com
重定向到 https://www.example.com
。
请推荐。
您应该能够通过使用 msg.req.headers.x-forwarded-proto
值然后发送具有 301 状态的响应来执行此操作,例如
if (msg.req.headers['x-forwarded-proto'] == 'http') {
msg.statusCode = 301;
msg.headers = {
location: 'https://' + msg.req.get('host') + msg.req.originalUrl
}
msg.payload = "";
return [null, msg]
} else {
return [msg, null]
}
如果你把它放在一个有 2 个输出的函数节点中,它将把重定向发送到第二个输出,否则将它发送到第一个输出。
您也可以通过在 settings.js
中添加自己的 httpNodeMiddleware
函数来做到这一点,如下所示:
....
httpNodeMiddleware: function(req, res, next){
if (req.headers['x-forwarded-proto'] == 'http') {
res.redirect(301, 'https://' + req.get('host') + req.originalUrl);
} else {
next();
}
},
...
此方法意味着您不需要流程中的任何重定向逻辑
旁注:
您可能需要测试 msg.req.headers['$wssc']
而不是 msg.req.headers.$wssc
.