简体   繁体   English

Laravel在负载均衡器+集中式Redis会话服务器下

[英]Laravel under a load balancer + centralized redis session server

I have 2 laravel nodes running in separate servers under a load balancer, and a dedicated redis server for session and cache storage. 我有2个laravel节点在负载均衡器下的单独服务器中运行,还有一个专用的Redis服务器用于会话和缓存存储。

I configured the session and cache drivers accordingly to "redis" and it connects just fine. 我将会话和缓存驱动程序相应地配置为“ redis”,并且连接正常。 I see files being stored inside the redis server. 我看到文件存储在Redis服务器中。

The issue is when I try to login, the page just gets refreshed without printing the "Invalid credential" errors that are normally stored in session. 问题是当我尝试登录时,页面只是刷新而没有打印通常存储在会话中的“无效凭据”错误。

Since the load balancer keeps redirecting from one node to another, the session is somehow getting lost. 由于负载均衡器不断从一个节点重定向到另一个节点,因此会话以某种方式丢失。 As a single instance it works just fine though. 作为单个实例,它可以正常工作。 Is there anyone having the same issue with laravel and load balancing? laravel和负载平衡是否存在相同的问题?

If there is a possible fix without configuring the balancer to use sticky sessions, that would be great! 如果没有配置平衡器以使用粘性会话就可以解决问题,那就太好了!

Thanks in advance! 提前致谢!

I think this package TrustedProxy solves your issue. 我认为此软件包TrustedProxy解决了您的问题。 Install it and then just add it to config/trustedproxy.php : 安装它,然后将其添加到config/trustedproxy.php

return [
'proxies' => [
    '192.168.10.10',
],

// These are defaults already set in the config:
'headers' => [
    (defined('Illuminate\Http\Request::HEADER_FORWARDED') ? Illuminate\Http\Request::HEADER_FORWARDED : 'forwarded') => 'FORWARDED',
    \Illuminate\Http\Request::HEADER_CLIENT_IP    => 'X_FORWARDED_FOR',
    \Illuminate\Http\Request::HEADER_CLIENT_HOST  => 'X_FORWARDED_HOST',
    \Illuminate\Http\Request::HEADER_CLIENT_PROTO => 'X_FORWARDED_PROTO',
    \Illuminate\Http\Request::HEADER_CLIENT_PORT  => 'X_FORWARDED_PORT',
]
];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM