2016-11-29 8 views
0

スタックは次のとおりです。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}!`); 
}); 

答えて

1

ソリューションが見つかり、これを試してみました!

コンテキスト:Herokuは、元のプロトコルを「X-Forwarded-Proto」というヘッダー変数に格納します。 HTTP Routing in Heroku Expressで 'req'オブジェクトに接続されているプロトコル変数ではなく、この変数をチェックする必要があります。あまりにも私のために働いた

//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); 
    } 
} 
+0

コード(つまり、代わりに)、req.protocolをチェックreq.get( 'X-転送さ-プロト' をチェックしません)!ありがとう! –

関連する問題