angularjs - Express.js 强制 HTTPS/SSL 重定向错误 : Too many redirects

标签 angularjs express heroku

所以堆栈是这样的:Express 和 Angular 部署到 Heroku。我正在尝试使用下面突出显示的代码使用简单的重定向来强制通过 HTTPS 为应用程序提供服务。但是,当我在浏览器中打开 URL 时,出现“重定向过多”错误。

当我输入http://[myurl].com时,我可以看到浏览器中的 URL 变为 https://[myurl].com但该地址没有显示任何内容。它只是显示“重定向太多”。这证明它成功地重定向了,但我觉得 Angular 把它搞砸了。

当我删除下面的重定向代码并在浏览器中手动访问 HTTPS 或 HTTP 地址时,它工作正常。

//HTTPS redirect middleware
function ensureSecure(req, res, next) {
    console.log(req.protocol + process.env.PORT + '' + req.hostname + req.url);
    if(req.secure || req.hostname=='localhost'){
        //Secure request, continue to next middleware
        next();
    }else{
        res.redirect('https://' + req.hostname + req.url);
        console.log(req.protocol + process.env.PORT + '' + req.hostname + req.url);
    }
}
//Parse the body of the request as a JSON object, part of the middleware stack (https://www.npmjs.com/package/body-parser#bodyparserjsonoptions)
app.use(bodyParser.json());
//Serve static Angular JS assets from distribution, part of the middleware stack, but only through HTTPS
app.all('*', ensureSecure);
app.use('/', express.static('dist'));
//Import routes
app.use('/api', [router_getToken, router_invokeBhApi]);
//Setup port for access
app.listen(process.env.PORT || 3000, function () {
    console.log(`The server is running on port ${process.env.PORT || 3000}!`);
});

这是您访问http://[myurl].com时的heroku日志示例(我屏蔽了网址):

2016-11-29T21:50:34.363391+00:00 app[web.1]: 0|app      | http37436[something].com/
2016-11-29T21:50:34.363468+00:00 app[web.1]: 0|app      | http37436[something].com/
2016-11-29T21:50:34.402022+00:00 app[web.1]: 0|app      | http37436[something].com/
2016-11-29T21:50:34.402091+00:00 app[web.1]: 0|app      | http37436[something].com/
2016-11-29T21:50:34.436006+00:00 app[web.1]: 0|app      | http37436[something].com/
2016-11-29T21:50:34.437454+00:00 app[web.1]: 0|app      | http37436[something].com/
2016-11-29T21:50:34.479580+00:00 app[web.1]: 0|app      | http37436[something].com/

浏览器(最新的 Chrome)在“网络”选项卡中一遍又一遍地显示这些请求:
'请求网址:https://[myurl].com/
请求方式:GET
状态代码:302 找到'

请注意 Heroku(express.js 代码中的 console.log)如何显示我正在发出 HTTP 请求,但我的浏览器却显示我正在发出 HTTPS 请求。好困惑!

编辑: 我也试过了

//HTTPS redirect middleware
function ensureSecure(req, res, next) {
    console.log(req.protocol + process.env.PORT + '' + req.hostname + req.url);
    if (req.secure || req.hostname == 'localhost') {
        //Serve Angular App
        express.static('dist');
    } else {
        //res.redirect('https://' + req.hostname + ':' + process.env.PORT + req.url);
        res.redirect('https://[myurl].com/');
    }
}
//Parse the body of the request as a JSON object, part of the middleware stack (https://www.npmjs.com/package/body-parser#bodyparserjsonoptions)
app.use(bodyParser.json());
//Serve static Angular JS assets from distribution, part of the middleware stack, but only through HTTPS
app.use('/', ensureSecure);
//Import routes
app.use('/api', [router_getToken, router_invokeBhApi]);
//Setup port for access
app.listen(process.env.PORT || 3000, function () {
    console.log(`The server is running on port ${process.env.PORT || 3000}!`);
});

最佳答案

找到解决方案!

上下文:Heroku 将原始协议(protocol)存储在名为“X-Forwarded-Proto”的 header 变量中。 HTTP Routing in Heroku您需要检查此变量,而不是与 Express 中的“req”对象关联的协议(protocol)变量。 (也就是说,不要检查 req.protocol,而是检查 req.get('X-Forwarded-Proto'))

代码:

//HTTPS redirect middleware
function ensureSecure(req, res, next) {
    //Heroku stores the origin protocol in a header variable. The app itself is isolated within the dyno and all request objects have an HTTP protocol.
    if (req.get('X-Forwarded-Proto')=='https' || req.hostname == 'localhost') {
        //Serve Angular App by passing control to the next middleware
        next();
    } else if(req.get('X-Forwarded-Proto')!='https' && req.get('X-Forwarded-Port')!='443'){
        //Redirect if not HTTP with original request URL
        res.redirect('https://' + req.hostname + req.url);
    }
}

关于angularjs - Express.js 强制 HTTPS/SSL 重定向错误 : Too many redirects,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40876599/

相关文章:

javascript - Node.js:无法存储 req.session 作为引用

python - 在环境 Python/Django 中存储配置

redirect - Heroku/GoDaddy : send naked domain to www

javascript - Angular js路由,500内部服务器错误

heroku - Heroku 上的 Rails 4 应用程序中未初始化的常量 Moped::BSON

javascript - 解析 ng-show 时出现 Angular 错误

javascript - TS2307 : Cannot find module 'angular'

angularjs - 模型更改时不会调用 ngModel.$render

angularjs - Angular UI $modal 组件转换

javascript - 将样式表添加到 Express 应用程序