强制将 heroku/angular 应用重定向到 HTTPS 版本

Sky*_*ker 0 javascript heroku node.js angular

我有一个 angular 5 应用程序,它托管在 heroku 上。目前用户可以访问该应用程序的 HTTP 版本。

即使用户访问 HTTP,如何强制用户重定向到 HTTPS 版本?

我尝试过的:

app.use(function (req, res, next) {
let sslUrl;

if (process.env.NODE_ENV === 'production' &&
    req.headers['x-forwarded-proto'] !== 'https') {

    sslUrl = ['https://myapp.herokuapp.com', req.url].join('');
    return res.redirect(sslUrl);
}

return next();
});
Run Code Online (Sandbox Code Playgroud)

我已经将上面的代码放在我的 node.js 服务器中,但它没有工作。

用户无法通过 HTTP 使用该应用程序,因为他们收到 403 错误

Sag*_*fan 5

我在https://github.com/rangle/force-ssl-heroku 上使用了“force-ssl-heroku”包,它的作用就像魔术一样,并且非常容易集成。

只需要在你的开始入口点脚本中:

var forceSsl = require('force-ssl-heroku');
Run Code Online (Sandbox Code Playgroud)

并像这样使用它:

app.use(forceSsl);
Run Code Online (Sandbox Code Playgroud)

部署和享受。

  • 并确保您的 NODE_ENV 在 Heruko 中设置为“生产”。 (2认同)