4

I have 1 load balancing server and 2 web servers: Server1 and Server2. Both of them connect with DB Server. My problem: I am using Yii session to control session. After I login success into Server1, loadbalancing server redirect to Server 2 => It's doesn't keep session that I login before (server1)

What can I do to keep session in Server2

4

2 回答 2

4

You have a couple of options:

1) If your load balancer supports it, you can enable session persistence so that the user always is sent to the same server as the one they originally hit. The benefit of this is that it's easy to setup if you don't want to change any code. The downside is that if one of your servers goes down you lose all your sessions on that node.

2) Setup a shared memcache (not memcached) session between node1 and node2. The relevant settings being.

php.ini

session.save_handler memcache
session.save_path tcp://<ip1>, tcp://<ip2>

memcache.ini

memcache.allow_failover 1
memcache.default_port   11211
memcache.hash_strategy  standard
memcache.max_failover_attempts  20

It's a little tricky to setup, but once you get it working you have full redundancy between both servers if one were to go down.

3) Setup a third node to manage sessions and configure php session.save_path to be that server's ip. The benefit of this is that sessions are now managed by a third server. The downside being you lose redundancy, if that server goes down you lose sessions.

于 2013-09-05T14:12:12.777 回答
2

A few other thoughts in addition to Charles' comments.

1) You can use CDbHttpSession instead of getting a memcached server setup separately. This will store your session in your DB server behind your web servers.

2) Do you have Suhosin enabled for your servers? If you do, it will cause many headaches, as Suhosin encrypts your session variables (and will do it differently on each server), causing your sessions to not be shared between servers.

Good luck and come back with more questions if you have them. Shifting to a load balancer took us a while as we worked through the bugs.

于 2013-09-05T17:25:53.663 回答