Dav*_*tti 2 ssl heroku node.js express
我最近在我的网站上添加了SSL证书.添加HTTPS支持后,我想将人们重定向到安全连接.我用谷歌搜索如何检测HTTPS,并在堆栈上找到许多不同的方式.它接缝他们为他人工作,但他们不适合我.在检查安全连接的4种方法中,只有一种有效.
什么对我不起作用:
什么有效:
req.headers ['x-forwarded-proto']是实际返回现实的唯一方式.
知道这一点后,我通过以下方式重定向到安全连接:
// Check if the connection is secure, if not, reddiret to a secure one.
function requireHTTPS(req, res, next) {
if(process.env.NODE_ENV == 'production') {
if (req.headers['x-forwarded-proto'] !== 'https') {
return res.redirect('https://' + req.get('host') + req.url);
}
}
next();
}
app.use(requireHTTPS);
Run Code Online (Sandbox Code Playgroud)
在上面提到的任何其他方式中,我会得到一个重定向循环,因为即使重定向到HTTPS之后,上述方法也会返回false或http.
我的问题是:为什么其他方法不起作用?
技术规格:
| 归档时间: |
|
| 查看次数: |
250 次 |
| 最近记录: |