nginx+tpmcat+redis实现session共享


版本:
nginx nginx-1.8.0.tar.gz
tomcat apache-tomcat-7.0.78.tar.gz
redis  redis-stable 4.0.1
安装包下载地址:链接:http://pan.baidu.com/s/1gfQqpPL 密码:xwpv
PS:redis在此用的是单节点,实际应用redis应该是集群形式


安装redis 网上教程有很多,最后需要注意的是配置redis外网访问


设置redis外网访问
配置文件中找到bind 127.0.0.1注释掉
再查找protected-mode yes 把yes修改为no
在查找daemonize 设置为no
/sbin/iptables -I INPUT -p tcp --dport 6379 -j ACCEPT      修改防火墙,使6379端口可以访问
/etc/rc.d/init.d/iptables save   保存防火墙修改命令

tomcat7安装


cd /usr/local/tomcat7
将安装包放入该目录下
tar xzf apache-tomcat-7.0.78.tar.gz解压文件
cd /usr/local/tomcat7
./bin/startup.sh启动tomcat
vi /etc/rc.d/rc.local 将/usr/local/tomcat7/apache-tomcat-7.0.78.tar.gz/bin/startup.sh放置在最下面     设置开机启动
/etc/init.d/iptables stop关闭防火墙 外网可以访问tomcat欢迎界面
相关命令:
关闭防火墙指令:/etc/init.d/iptables stop
永久关闭防火墙指令:chkconfig iptables off
开启防火墙指令:/etc/init.d/iptables start
永久开启防火墙指令:chkconfig iptableson
查看防火墙状态:/etc/init.d/iptables status

修改tomcat/conf/context.xml文件在</context>上面添加
<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve"/>
   <Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"
        host="192.168.4.53"         #这里是redis的ip地址
        port="6379" #这里是redis的端口号
        database="0"                #这里是redis的数据库选择
         maxInactiveInterval="60"/> #设置session的最大保存时间
PS(如果redis有密码。需要再加一行password:"123456")
 
在tomcat/bin下面导入jar包    jar包地址为:链接:http://pan.baidu.com/s/1o881d5g 密码:jwee

创建示例程序,分别放到对应ip的tomcat下面,启动tomcat
<body>  
   这是ip192.168.4.51的界面 
   <%= session.getId() %>  
</body> 
<body>  
   这是ip192.168.4.53的界面 
   <%= session.getId() %>  
</body>
访问http://192.168.4.53:8080//SessionTest/index.jsp    http://192.168.4.51:8080//SessionTest/index.jsp
都可以看到对应的界面,但是IP不一样

配置nginx
1.安装教程不再讲述,安装教程:链接:http://pan.baidu.com/s/1kUEwCSZ 密码:4b7d
2.上述教程需要的配置文件地址:链接:http://pan.baidu.com/s/1jIqYugu 密码:dvnx
3.配置文件nginx.conf文件    
upstream myServer {  
   
   server 192.168.4.51:8080;  
   server 192.168.4.53:8080;  
   }  #配置tomcat的IP地址和端口号
   
   #gzip  on;  
    #监听88 端口  
   server {  
       listen       88;  
       #定义要使用的域名  
       server_name  localhost;  
   
       #charset koi8-r;  
      #设定本虚拟主机的访问日志  
       #access_log logs/host.access.log  main;  
       #配置前缀  即要转发到的ip+port  
       location / {  
           proxy_pass  http://myServer;  #引用upstream myServer
          # root   html;   #注释nginx的欢迎界面
          # index  index.html index.htm;  
       } 


nginx的IP地址为192.168.4.52 端口为88
nginx配置成功之后。http://192.168.4.52:88,可以访问到tomcat的欢迎界面
http://192.168.4.52:88/SessionTest/index.jsp      刷新,可以看到IP会变化,但是session ID的值一直保持不变,实现session共享。

本篇CSDN地址:http://blog.csdn.net/chenjian123654/article/details/77895336
参考资料地址:
http://blog.csdn.net/zhanghongjie0302/article/details/50550289
http://oceanszf.blog.51cto.com/6268931/1752641
http://zihai367.iteye.com/blog/2271371
http://blog.csdn.net/qq_16216221/article/details/72599016

posted on 2017-09-11 09:08  Tilefish  阅读(374)  评论(0编辑  收藏  举报