在负载均衡器后面使用 Laravel

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17074463/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 07:57:03  来源:igfitidea点击:

Using Laravel behind a load balancer

phpsessionlaravelload-balancing

提问by thestepafter

I have been working on a Laravel 4 site for awhile now and the company just put it behind a load balancer. Now when I try to login it basically just refreshes the page. I tried using fideloper's proxy package at https://github.com/fideloper/proxybut see no change. I even opened it up to allow all IP addresses by doing proxies => '*'. I need some help with knowing what needs to be done to get Laravel to work behind a load balancer, especially with sessions. Please note that I am using the database Laravel session driver.

我已经在 Laravel 4 站点上工作了一段时间,该公司只是将它放在负载均衡器后面。现在,当我尝试登录时,它基本上只是刷新页面。我尝试在https://github.com/fideloper/proxy 上使用 fideloper 的代理包,但没有看到任何变化。我什至通过代理 => '*' 打开它以允许所有 IP 地址。我需要一些帮助来了解需要做什么才能让 Laravel 在负载均衡器后面工作,尤其是会话。请注意,我使用的是数据库 Laravel 会话驱动程序。

The load balancer is a KEMP LM-3600.

负载平衡器是 KEMP LM-3600。

回答by thestepafter

Thank you to everyone for the useful information you provided. After further testing I found that the reason this wasn't working is because we are forcing https through the load balancer, but allowing http when not going through the load balancer. The login form was actually posting to http instead of https. This allowed the form to post but the session data never made it back to the client. Changing the form to post to https fixed this issue.

感谢大家提供的有用信息。经过进一步测试,我发现这不起作用的原因是我们强制 https 通过负载均衡器,但在不通过负载均衡器时允许 http。登录表单实际上是发布到 http 而不是 https。这允许表单发布,但会话数据从未返回给客户端。将表单更改为发布到 https 解决了此问题。

回答by amaster

We use a load balancer where I work and I ran into similiar problems with accessing cPanel dashboards where the page would just reload every time I tried accessing a section and log me off as my IP address was changing to them. The solution was to find which port cPanel was using and configure the load balancer to bind that port to one WAN. Sorry, I am not familiar with laravel and if it just using port 80 then this might not be a solution.

我们在我工作的地方使用负载平衡器,我在访问 cPanel 仪表板时遇到了类似的问题,每次我尝试访问一个部分时页面都会重新加载,并在我的 IP 地址更改为它们时将我注销。解决方案是找到 cPanel 使用的端口并配置负载平衡器以将该端口绑定到一个 WAN。抱歉,我对 laravel 不熟悉,如果它只使用端口 80,那么这可能不是解决方案。

回答by Sven

Note that the session handling in Laravel 4 uses Symfony 2 code, which lacks proper session lockingin all self-coded handlers that do not use the PHP provided session save handlers like "files", "memcached" etc.

请注意,Laravel 4 中的会话处理使用Symfony 2 代码,该代码在所有不使用 PHP 提供的会话保存处理程序(如“文件”、“memcached”等)的自编码处理程序中缺乏适当的会话锁定

This will create errors when used in a web application with parallel requests like Ajax, but this should occur unrelated to any load balancer.

这将在具有并行请求(如 Ajax)的 Web 应用程序中使用时产生错误,但这应该与任何负载均衡器无关。

You really should do some more investigation. HTTP load balancers do have some impact on the information flow, but the only effect on a PHP application would be that a single user surfing the site will randomly send the requests to any one of the connected servers, and not always to the same.

你真的应该做更多的调查。HTTP 负载平衡器确实对信息流有一些影响,但对 PHP 应用程序的唯一影响是,浏览该站点的单个用户会随机将请求发送到任何一个连接的服务器,而不总是发送到相同的服务器。

Do you also use any fancy database setup, like master-slave replication? This would affect sessions more likely, if the writing is only done on the master, the reading is done only on a slave, and this slave is behind the master with updating the last write operation. Such a configuration is not recommended as a session storage. I'd rather use Memcached instead. The PHP session save handler does implement proper locking as well...

您是否还使用任何花哨的数据库设置,例如主从复制?这将更有可能影响会话,如果写入仅在主机上完成,读取仅在从机上完成,并且该从机在更新最后一次写入操作时落后于主机。不推荐这样的配置作为会话存储。我宁愿使用 Memcached。PHP 会话保存处理程序也实现了正确的锁定......

Using fideloper's proxy does not make sense. A load balancer should be transparent to the web server, i.e. it should not act as a reverse proxy unless configured to do so.

使用 fideloper 的代理没有意义。负载平衡器应该对 Web 服务器透明,即它不应充当反向代理,除非配置为这样做。

回答by Collin James

Use a shared resource to store the session data. File and memcached will surely not work. DB should be OK. That's what I'm using on a load balanced setup with a common database.

使用共享资源来存储会话数据。File 和 memcached 肯定不会工作。DB应该没问题。这就是我在具有公共数据库的负载平衡设置中使用的内容。

回答by Mustafa Dwekat

I have been using TrustedProxyfor a while now and its working fine.

我已经使用TrustedProxy一段时间了,它工作正常。

the main issue with load balancers is proxy routing. the next is from the readme file and its what I was looking for.

负载平衡器的主要问题是代理路由。下一个来自自述文件及其我正在寻找的内容。

If your site sits behind a load balancer, gateway cache or other "reverse proxy", each web request has the potential to appear to always come from that proxy, rather than the client actually making requests on your site.

To fix that, this package allows you to take advantage of Symfony's knowledge of proxies. See below for more explanation on the topic of "trusted proxies".

如果您的站点位于负载均衡器、网关缓存或其他“反向代理”之后,则每个 Web 请求都有可能看起来总是来自该代理,而不是客户端实际在您的站点上发出请求。

为了解决这个问题,这个包允许你利用 Symfony 的代理知识。有关“受信任的代理”主题的更多解释,请参见下文。