本文介绍了Heroku NodeJS http到https ssl强制重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序在heroku上运行,并使用https与节点快速运行。我如何确定在heroku上强制重定向到https与nodejs的协议?



我的应用程序只是一个简单的http服务器,它还没有实现heroku发送https请求:

  / * Heroku在这个环境变量中提供了他们想要的端口(提示:它不是80 )* / 
app.listen(process.env.PORT || 3000);


解决方案

答案是使用' Heroku向前传递的原始转发,因为它是代理thingamabob。 (注意:他们还传递了其他几个x-变量,可能很方便,检查它们

我的代码:

  / *在顶部,在其他路由之前使用其他重定向方法* / 
app.get('*',function(req,res,next){
if(req.headers ['x-forwarded- proto']!='https')
res.redirect('https://mypreferreddomain.com'+req.url)
else
next()/ *继续其他路由if我们没有重定向* /
})

感谢布兰登,只是在等待6小时延迟的事情,不会让我回答自己的问题。


I have an application up and running on heroku with express on node with https,. How do I identify the protocol to force a redirect to https with nodejs on heroku?

My app is just a simple http server, it doesn't (yet) realize heroku is sending it https requests:

/* Heroku provides the port they want you on in this environment variable (hint: it's not 80) */
app.listen(process.env.PORT || 3000);
解决方案

The answer is to use the header of 'x-forwarded-proto' that Heroku passes forward as it does it's proxy thingamabob. (side note: They pass several other x- variables too that may be handy, check them out).

My code:

/* At the top, with other redirect methods before other routes */
app.get('*',function(req,res,next){
  if(req.headers['x-forwarded-proto']!='https')
    res.redirect('https://mypreferreddomain.com'+req.url)
  else
    next() /* Continue to other routes if we're not redirecting */
})

Thanks Brandon, was just waiting for that 6 hour delay thing that wouldn't let me answer my own question.

这篇关于Heroku NodeJS http到https ssl强制重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 13:55