Yii session do not work in multi server

前端 未结 2 1732
醉酒成梦
醉酒成梦 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:24

    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.

    0 讨论(0)
  • 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://<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.

    0 讨论(0)
提交回复
热议问题