Express.js强制HTTPS / SSL重定向错误:重定向太多

时间:2016-11-29 22:02:53

标签: angularjs express heroku

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

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

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

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/

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

//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}!`);
});

浏览器(Chrome最新版)在“网络”标签中显示这些请求。过度&再一次:
'请求网址:https://[myurl].com/
请求方法:GET
状态代码:302找到'

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

编辑: 我也试过这个

{{1}}

3 个答案:

答案 0 :(得分:8)

找到了解决方案!

上下文:Heroku将原始协议存储在称为' X-Forwarded-Proto'的头变量中。 HTTP Routing in Heroku您需要检查此变量,而不是与“请求”相关联的协议变量。 Express中的对象。 (也就是说,不要检查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);
    }
}

答案 1 :(得分:2)

您可以通过选项

{ trustProtoHeader: true }

到HTTPS()方法;例如:

const enforce = require('express-sslify');
app.use(enforce.HTTPS({ trustProtoHeader: true }));

那为我解决了

答案 2 :(得分:0)

在使用自定义中间件对Heroku进行过多重定向时,我也遇到了麻烦。通过使用 express-force-https 解决了该问题。很好,它可以检测是否在localhost上运行并禁用开发。

Motor         # litteral string "Motor"
.*            # 0 to n characters
Fans          # litteral string "Fans"
\D*           # 0 to n non-digit characters
9             # a litteral "9"

https://www.npmjs.com/package/express-force-https