スタックは次のとおりです。Express &角度がHerokuに展開されています。私は、HTTPS経由で強制されたアプリケーションを、以下で強調表示されたコードを使って簡単なリダイレクトを使って提供しようとしています。しかし、ブラウザでURLを開くと、「リダイレクトが多すぎます」というエラーが表示されます。Express.js強制HTTPS/SSLリダイレクトエラー:リダイレクトが多すぎる
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を訪問したときにこれが(私はURLをマスクし)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/
ブラウザ(クローム最新)は&上&上で、[ネットワーク]タブでこれらの要求を示しています何度も何度:
'リクエストURL:https://[myurl].com/
リクエスト方法:
ステータスコードを取得:302見つけた'
Heroku(express.jsコードのconsole.log)がHTTP要求を行っているが、ブラウザにHTTPS要求を出していると表示されていることに注目してください。混乱している!
EDIT: は私も
//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}!`);
});
:
コード(つまり、代わりに)、req.protocolをチェックreq.get( 'X-転送さ-プロト' をチェックしません)!ありがとう! –