思路:把session存放到一个公共redis服务器上
每次浏览器请求服务端都会带上cookie,因为使用的是权重负载均衡方案,因此nginx反向代理服务器会把请求发放到不同的服务端,服务端用cookie,可以去公共redis服务器取session值
server1:
<?php ini_set("session.save_handler", "redis"); ini_set("session.save_path", "tcp://192.168.174.132:6379"); session_start(); echo "server1"; if(isset($_SESSION['uid'])) { echo $_SESSION['uid']; }else{ echo "no"; $_SESSION['uid'] = 1; }
server2:
<?php ini_set("session.save_handler", "redis"); ini_set("session.save_path", "tcp://192.168.174.132:6379"); session_start(); echo "server2"; if(isset($_SESSION['uid'])) { echo $_SESSION['uid']; }else{ echo "no"; $_SESSION['uid'] = 1; }