Laravel 5.4相对而不是绝对302重定向 [英] Laravel 5.4 relative instead of absolute 302 redirects

查看:77
本文介绍了Laravel 5.4相对而不是绝对302重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在负载均衡器后面的新Laravel应用出现问题. 我想让Laravel将Auth中间件302重定向到类似/login 的相对路径,而不是 http: //myappdomain.com /登录实际上在做.

I'm having issues with a new Laravel app behind a load balancer. I would like to have Laravel do the Auth middleware 302 redirects to relative path like /login instead of the http://myappdomain.com/login is actually doing.

我只在默认的.htaccess Laravel发行版中看到301重定向,这使我相信Laravel内部的行为正确,对吗?

I only see 301 redirects in the default .htaccess Laravel ships which makes me believe the behavior is right within Laravel, am I wrong?

有人可以指出我正确的方向吗?

Can someone point me in the right direction?

推荐答案

如果在负载均衡器后面需要正确确定请求是否安全,则需要让框架知道您在代理后面.这将确保route()url()助手能够生成正确的URL,并消除了创建相对重定向的需求,这些相对重定向既不受浏览器的100%支持,也无法在从子路径提供网页时正常工作.

If you need to properly determine whether a request was secure when behind a load balancer you need to let the framework know that you're behind a proxy. This will ensure that the route() and url() helpers generate correct URLs and remove the need to create relative redirects which are both not 100% supported by browsers and also won't work properly when serving a webpage from a sub-path.

这是我们用来解决此问题的方法,到目前为止对我们来说一直有效:

This is what we use to solve this problem and it's working so far for us:

.env

LOAD_BALANCER_IP_MASK=aaa.bbb.ccc.ddd/xx #Subnet mask

LoadBalanced中间件

LoadBalanced Middleware

class LoadBalanced { 
      public function handle($request, $next) {
          if (env("LOAD_BALANCER_IP_MASK")) {
             $request->setTrustedProxies([ env("LOAD_BALANCER_IP_MASK") ]);
          }
          $next($request);
     }
}

然后将中间件放入您的Kernel.php:

Then put the middleware in your Kernel.php:

protected $middleware = [ 
    LoadBalanced::class,
    //.... It shouldn't matter if it's first or last as long as other global middleware don't need it

];

这是Laravel可用的功能,因为它使用Symfony请求作为基础.负载平衡器转发一些重要的标头是如何工作的. Symfony目前了解:

This is a feature available to Laravel because it is using the Symfony request as a base. How this work is that the load balancer forwards some important headers. Symfony currently understands:

 protected static $trustedHeaders = array(
    self::HEADER_FORWARDED => 'FORWARDED',
    self::HEADER_CLIENT_IP => 'X_FORWARDED_FOR',
    self::HEADER_CLIENT_HOST => 'X_FORWARDED_HOST',
    self::HEADER_CLIENT_PROTO => 'X_FORWARDED_PROTO',
    self::HEADER_CLIENT_PORT => 'X_FORWARDED_PORT',
);

其中包含有关向负载均衡器发出请求的用户的信息以及所使用的协议.

which have information regarding the user making the request to the load balancer and the protocol used.

也根据框架评论:

FORWARDED标头是 rfc7239 的标准.

其他标头是非标准的,但已广泛使用 常见的反向代理(例如Apache mod_proxy或Amazon EC2).

The other headers are non-standard, but widely used by popular reverse proxies (like Apache mod_proxy or Amazon EC2).

这篇关于Laravel 5.4相对而不是绝对302重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆