Laravel 5 aws ec2 重定向循环上的中间件 https

Laravel 5 middleware https on aws ec2 redirect loop

基于 我在我的应用程序中添加了相同的中间件,结果是重定向循环。

如果用户未通过身份验证,应用程序将重定向到登录页面,所以对我来说这就是问题所在。或许不是。

ssl 证书已正确安装并且有效(如果我手动转到 https://myapp.org 它会按预期工作)。

主要问题是我在整个应用程序中使用助手 url() 来生成 urls,现在我需要重定向以保护 urls。

我尝试删除那个中间件,并将重定向添加到 .htaccess,但结果是一样的(这个网页有一个重定向循环)。

更新 我尝试调试问题,看起来没有重定向到 https,这就是重定向循环的原因。我为 headers 添加了日志......

{
  "host": [
    "myapp.org"
  ],
  "accept": [
    "text\/html,application\/xhtml+xml,application\/xml;q=0.9,image\/webp,*\/*;q=0.8"
  ],
  "user-agent": [
    "Mozilla\/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/41.0.2272.76 Safari\/537.36"
  ],
  "accept-encoding": [
    "gzip, deflate, sdch"
  ],
  "accept-language": [
    "en-US,en;q=0.8"
  ],
  "cookie": [
    "XSRF-TOKEN=..."
  ],
  "x-forwarded-for": [
    "IP ADDRESS"
  ],
  "x-forwarded-host": [
    "myapp.org"
  ],
  "x-forwarded-server": [
    "ip-IP.compute.internal"
  ],
  "connection": [
    "Keep-Alive"
  ]
}

我也在记录 $request->secure() 的值,它总是错误的。

这是我的中间件(日志的结果在每行日志旁边的注释中)

<?php namespace App\Http\Middleware;

use Closure;
use Log;

class HttpsProtocol
{
  /**
   * Redirect to https.
   *
   * @param  \Illuminate\Http\Request  $request
   * @param  \Closure  $next
   *
   * @return mixed
   */
  public function handle($request, Closure $next)
  {
    if(!$request->secure() && app()->environment() === 'production') {
      Log::info(app()->environment()); // "production"
      Log::info($request->secure()); // ""
      Log::info($request->header('x-forwarded-proto')); // ""
      Log::info(json_encode($request->header())); // The json just posted
      Log::info($request->getRequestUri()); // "/auth/login"
      return redirect()->secure($request->getRequestUri());
    }

    return $next($request);
  }

}

我的应用程序在 Elastic Beanstalk (EC2 single-instance) 中。 如有任何帮助,我们将不胜感激。

有一个 package and a gist 解决了上述问题。