Nginx + Tomcat + Session
2014-12-10 09:58
381 查看
分别下载
tomcathttp://archive.apache.org/dist/tomcat/tomcat-7/v7.0.22/bin/apache-tomcat-7.0.22.tar.gz
nginxhttp://nginx.org/download/nginx-1.1.14.tar.gz
准备两个虚拟机:
server1192.168.1.112
server2192.168.1.64
Tomcat直接解压,运行,使用默认的8080端口
访问http://192.168.1.112:8080和http://192.168.1.64:8080出现Tomcat首页即可
接下来安装nginx,nginx我就直接安装在server1上
nginx_upstream_jvm_route是一个Nginx的扩展模块,用来实现基于Cookie的SessionSticky的功能,去SVN下载最新版
解压nginx
运行
在nginx安装目录下的conf/目录新建一个文件proxy.conf(/etc/nginx/conf/proxy.conf),内容如下:
修改nginx配置文件/etc/nginx/conf/nginx.conf
完整配置如下
具体配置说明请参考《轻量级WEB服务器Nginx》
然后运行nginx
访问http://192.168.1.112/NginxStatus,可以看到nginx状态
然后修改tomcat配置文件,打开apache-tomcat-7.0.22/conf/server.xml,找到最下面的<Enginename="Catalina"defaultHost="localhost">节点,修改为
server1
并插入如下配置
server2
在两个tomcat的webapps目录下分别新建一个项目test,
/test
/test/index.jsp
/test/WEB-INF/
/test/WEB-INF/web.xml
index.jsp内容
web.xml
注意上面的distributable节点,表示该应用是在集群环境下的。
重启tomcat后在两台机子上访问http://192.168.1.112/test
从SessionId后面带的服务器名可以看到负载均衡的效果。
然后将server1上的tomcat停掉,再刷新页面,可以看到原来访问server1的页面成功地切换到了server2,而sessionId没有变,session中的msg也和原来一样。
tomcat
nginx
准备两个虚拟机:
server1192.168.1.112
server2192.168.1.64
Tomcat直接解压,运行,使用默认的8080端口
接下来安装nginx,nginx我就直接安装在server1上
nginx_upstream_jvm_route是一个Nginx的扩展模块,用来实现基于Cookie的SessionSticky的功能,去SVN下载最新版
svncheckouthttp://nginx-upstream-jvm-route.googlecode.com/svn/trunk//root/dev/nginx-upstream-jvm-route-read-only
解压nginx
tarzxvfnginx-1.1.14.tar.gz cdnginx-1.1.14
运行
patch-p0</root/dev/nginx-upstream-jvm-route-read-only/jvm_route.patch
./configure--prefix=/etc/nginx--with-http_stub_status_module--add-module=/root/dev/nginx-upstream-jvm-route-read-only/
make
makeinstall
在nginx安装目录下的conf/目录新建一个文件proxy.conf(/etc/nginx/conf/proxy.conf),内容如下:
proxy_redirectoff; proxy_set_headerHost$host; proxy_set_headerX-Real-IP$remote_addr; proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for; client_max_body_size10m; client_body_buffer_size128k; proxy_connect_timeout90; proxy_send_timeout90; proxy_read_timeout90; proxy_buffer_size4k; proxy_buffers432k; proxy_busy_buffers_size64k; proxy_temp_file_write_size64k;
修改nginx配置文件/etc/nginx/conf/nginx.conf
完整配置如下
http{ includemime.types; #反向代理配置 includeproxy.conf; default_typeapplication/octet-stream; sendfileon;
keepalive_timeout65; upstreamserver1{ server192.168.1.112:8080srun_id=tomcat1; server192.168.1.64:8080srun_id=tomcat2; jvm_route$cookie_JSESSIONID|sessionidreverse; } server{ listen80; server_namelocalhost; access_log/var/log/nginx/access.log; location~^/NginxStatus/{ stub_statuson;#Nginx状态监控配置 access_logoff; } location~^/(WEB-INF)/{ denyall; } location/{ roothtml; indexindex.htmlindex.htm; proxy_passhttp://server1;} location/doc{ root/usr/share; autoindexon; allow127.0.0.1; denyall; } location/images{ root/usr/share; autoindexon; }
具体配置说明请参考《轻量级WEB服务器Nginx》
然后运行nginx
/etc/nginx/sbin/nginx
访问http://192.168.1.112/NginxStatus,可以看到nginx状态
然后修改tomcat配置文件,打开apache-tomcat-7.0.22/conf/server.xml,找到最下面的<Enginename="Catalina"defaultHost="localhost">节点,修改为
server1
<Enginename="Catalina"defaultHost="localhost"jvmRoute="tomcat1">
并插入如下配置
<ClusterclassName="org.apache.catalina.ha.tcp.SimpleTcpCluster" channelSendOptions="8"> <ManagerclassName="org.apache.catalina.ha.session.DeltaManager" expireSessionsOnShutdown="false" notifyListenersOnReplication="true"/> <ChannelclassName="org.apache.catalina.tribes.group.GroupChannel"> <MembershipclassName="org.apache.catalina.tribes.membership.McastService" address="224.0.0.4" port="45564" frequency="500" dropTime="3000"/> <ReceiverclassName="org.apache.catalina.tribes.transport.nio.NioReceiver" address="192.168.1.112" port="4000" autoBind="100" selectorTimeout="5000" maxThreads="6"/> <SenderclassName="org.apache.catalina.tribes.transport.ReplicationTransmitter"> <TransportclassName="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/> </Sender> <InterceptorclassName="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/> <InterceptorclassName="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/> <InterceptorclassName="org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor"/> </Channel> <ValveclassName="org.apache.catalina.ha.tcp.ReplicationValve" filter=""/> <ValveclassName="org.apache.catalina.ha.session.JvmRouteBinderValve"/> <ClusterListenerclassName="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/> <ClusterListenerclassName="org.apache.catalina.ha.session.ClusterSessionListener"/> </Cluster>
server2
...
<Enginename="Catalina"defaultHost="localhost"jvmRoute="tomcat2"> <ClusterclassName="org.apache.catalina.ha.tcp.SimpleTcpCluster" channelSendOptions="8"> <ManagerclassName="org.apache.catalina.ha.session.DeltaManager" expireSessionsOnShutdown="false" notifyListenersOnReplication="true"/> <ChannelclassName="org.apache.catalina.tribes.group.GroupChannel"> <MembershipclassName="org.apache.catalina.tribes.membership.McastService" address="224.0.0.4" port="45564" frequency="500" dropTime="3000"/> <ReceiverclassName="org.apache.catalina.tribes.transport.nio.NioReceiver" address="192.168.1.64" port="4000" autoBind="100" selectorTimeout="5000" maxThreads="6"/> <SenderclassName="org.apache.catalina.tribes.transport.ReplicationTransmitter"> <TransportclassName="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/> </Sender> <InterceptorclassName="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/> <InterceptorclassName="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/> <InterceptorclassName="org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor"/> </Channel> <ValveclassName="org.apache.catalina.ha.tcp.ReplicationValve" filter=""/> <ValveclassName="org.apache.catalina.ha.session.JvmRouteBinderValve"/> <ClusterListenerclassName="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/> <ClusterListenerclassName="org.apache.catalina.ha.session.ClusterSessionListener"/> </Cluster>
...
jvmRoute="tomcat1"和nginx配置upstream中的srun_id对应
membership中的address=224.0.0.4为组播IP,集群中的tomcat通信之用
receiver中的address设为本机IP,或auto,如果多个Tomcat在同一台电脑上,则要保证port端口不重复
在两个tomcat的webapps目录下分别新建一个项目test,
/test
/test/index.jsp
/test/WEB-INF/
/test/WEB-INF/web.xml
index.jsp内容
<%@pagelanguage="java"contentType="text/html;charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDHTML4.01Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=UTF-8">
<title>Inserttitlehere</title>
</head>
<body>
<%=session.getId()%><br>
<%
Stringmsg=(String)session.getAttribute("msg");
if(null==msg){
session.setAttribute("msg","Hello!");
}else{
session.setAttribute("msg",msg+0);
}
%>
<%=session.getAttribute("msg")%>
</body>
</html>
web.xml
<?xmlversion="1.0"encoding="UTF-8"?>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<distributable/>
</web-app>
注意上面的distributable节点,表示该应用是在集群环境下的。
重启tomcat后在两台机子上访问http://192.168.1.112/test
从SessionId后面带的服务器名可以看到负载均衡的效果。
然后将server1上的tomcat停掉,再刷新页面,可以看到原来访问server1的页面成功地切换到了server2,而sessionId没有变,session中的msg也和原来一样。
相关文章推荐
- tomcat+nginx+redis实现均衡负载、session共享
- nginx+tomcat实现集群负载均衡(实现session复制)
- (转)tomcat+nginx+redis实现均衡负载、session共享(二)
- nginx集群使用session粘连Tomcat故障后的会话转移解决策略
- tomcat集群共享session(nginx+redis)
- Nginx和Tomcat负载均衡实现session共享(转)
- nginx+tomcat+memcached (msm)实现 session同步复制
- tomcat+nginx+redis实现均衡负载、session共享(二)
- Nginx+Tomcat+Memcached实现tomcat集群和session共享
- nginx-tomcat负载均衡redis-session共享,静态资源分离
- tomcat+nginx+redis实现均衡负载、session共享
- nginx+tomcat+redis的集群+session共享
- Nginx和Tomcat负载均衡实现session共享
- Nginx+Tomcat+Memcached集群Session共享
- Nginx+Tomcat+Memcache实现负载均衡及Session共享
- session共享之nginx+tomcat+redis
- Nginx+tomcat+memcached缓存共享session
- Nginx和Tomcat负载均衡实现session共享
- Nginx + tomcat + session 同步
- linux下nginx+tomcat+msm+memcached实现session共享(测试)