通过Yii会话进行负载均衡 [英] Load balancing with Yii sessions

查看:85
本文介绍了通过Yii会话进行负载均衡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Yii 1.1.14应用程序配置负载平衡的环境,但是我似乎遇到了这样的问题:当负载平衡器使用另一个节点时,Yii无法使用户保持登录状态.在大多数情况下,登录时,它会要求用户登录两次,因为它仅在一个节点上登录,然后在另一节点上加载页面.否则,它将要求用户在浏览中途再次登录.

I am trying to configure a load-balanced environment using Yii 1.1.14 applications, but I seem to be having the problem where Yii does not keep a user logged in when the load balancer uses another node. Most of the time, when logging in, it will ask the user to login twice because it only logs in on one node, and then loads the page on another. Otherwise, it will ask the user to login again half-way through browsing.

该应用程序正在使用数据库会话,并且可以看到数据库中的到期时间正在更新.即使在要求他们登录后立即再次登录的情况下,会话过期时间也会在数据库中更新. Yii对会话进行服务器依赖吗?

The application is using DB sessions and I can see that the expire time is being updated in the database. Even in the case when it asks them to login again straight after they have already logged in, the session expire time is updated in the database. Does Yii do anything server dependent with the sessions?

我已经搜索了几个小时,但是在这个主题上找不到很多东西,想知道是否还有其他人遇到过这样的问题.

I've searched around for hours but unable to find much on this topic, and wondering if anyone else has come across such problem.

在服务器端,我将Nginx与PHP-FPM和Amazon的ELB用作负载平衡器.解决方法(作为最后的解决方法)是在负载平衡器上使用粘性会话,但是如果节点脱机并强制用户使用备用节点,则这样做不能达到最佳效果.

On the server-side, I am using Nginx with PHP-FPM and Amazon's ELB as the load balancer. The work around (as a last resort) is to use sticky sessions on the load balancer, but then this does not work the best if a node was to go offline and force the user to use an alternative node.

如果需要进一步说明,请告诉我.

Please let me know if I need to clarify anything better.

推荐答案

问题是,用于生成应用程序ID的基本路径(在会话中以身份验证信息为前缀)在每个服务器上都不匹配. Amazon OpsWorks使用相同的符号链接路径将代码部署到服务器,但是由于版本控制和符号链接,PHP返回的实际路径有所不同.

The issue was that the base path which was used to generate the application ID, prefixed to the authentication information in the session, did not match on each server. Amazon OpsWorks was deploying the code to the servers using the identical symlinked path, but the real path returned by PHP differed due to versioning and symlinking.

例如,两台服务器上的符号链接路径均为"/app/current".但是,一台服务器上的实际路径是"/app/releases/2014010700",另一台服务器是"/app/releases/2014010701",这会生成不同的哈希,因此无法使用该会话.

For example, the symlink path on both servers was '/app/current'. However, the actual path on one server was '/app/releases/2014010700' and the other was '/app/releases/2014010701', which was generating a different hash and therefore not working with the session.

在我的配置文件中更改基本路径以使用符号链接路径可以解决该问题,而在使用dirname()之前,该方法返回了符号链接内容的真实路径.我还必须在Yii框架的setBasePath中删除realpath()函数.

Changing the base path to use the symlink path in my configuration file fixed the problem, whereas before it was using dirname() which was returning the real path of the symlinked contents. I also had to remove the realpath() function in setBasePath in the Yii framework.

我对Yii框架所做的修改是针对我的问题的,但是对于遇到多个节点类似问题的其他人,我会仔细检查以确保每个节点都在完全相同的路径中包含该应用程序.

The modifications I made to the Yii framework are quite specific for my issue, but for anyone else experiencing a similar issue with multiple nodes, I would double check to ensure each node contains the application in the exact same path.

感谢以下文章: http://www.yiiframework.com/forum/index.php/topic/19574-multi-server-authentication-failure-with-db-sessions

这篇关于通过Yii会话进行负载均衡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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