我有一个在 Heroku 上运行的 NodeJS HTTP(非 S)服务器。我配置了 SSL,并且它接受对 HTTPS 的请求。我使用普通 HTTP 服务器的原因是 the following :
SSL termination occurs at Heroku's load balancers; they send your app plain (non-SSL) traffic, so your app should create a non-HTTPS server."
不幸的是,我的应用程序仍然响应普通的 HTTP 请求。我想强制从 HTTP 重定向到 HTTPS。我可以使用一些中间件来做到这一点:
/* 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 */
})
但是这是一个好的解决方案吗? POST 请求如何工作?如果我发布到 HTTP,是否应该允许?
我想到的另一种 final方法是使用 Nginx 并在其中粘贴从 HTTP 到 HTTPS 的重定向。不幸的是 Heroku 不允许 Nginx 配置。
最佳答案
最好使用 app.use(function(req, res, next{/* ... */});
来捕获其他 HTTP 方法。
关于node.js - HTTPS NodeJS 和 Heroku。强制使用 HTTPS?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24643286/