Yii session do not work in multi server

前端 未结 2 1742
醉酒成梦
醉酒成梦 2020-12-17 05:54

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 lo

2条回答
  •  忘掉有多难
    2020-12-17 06:43

    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://, tcp://
    

    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.

提交回复
热议问题