1. 程式人生 > >nginx+Tomcat反向代理實現session會話保持

nginx+Tomcat反向代理實現session會話保持

tomcat session 會話保持

環境:
nginx+tomcat
一臺nginx:192.168.2.198
一臺tomcat1:192.168.2.197
一臺tomcat2:192.168.2.199

一、Tomcat上操作(2臺機器相同操作)
1、jdk安裝
2、tomcat安裝
3、配置path環境變量
4、啟動tomcat
5、創建測試頁面並測試頁面
[root@localhost conf]#vi /usr/local/tomcat_a/webapps/test/index.jsp
<%@ page language="java" %>
<html>
<head><title>TomcatA</title></head>

<body>
<h1><font color="red">TomcatA.linuxinfo.top</font></h1>
<tablealign="centre" border="1">
<tr>
<td>SessionID</td>
<% session.setAttribute("linuxinfo.top","linuxinfo.top");%>
<td><%=session.getId() %></td>
</tr>
<tr>
<td>Createdon</td>
<td><%=session.getCreationTime() %></td>
</tr>
</table>
</body>
</html>
6、訪問192.168.2.197,測試如下,查看session ID
技術分享圖片
訪問192.168.2.199,測試如下,查看session ID
技術分享圖片

二、基於tomcat群集配置session會話保持
1、編輯vi server.xml ,在Engine字段中添加如下代碼,開啟群集服務
<Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1"> #jvmRoute表示唯一表示本機,所以不同主機的是不同的。這個參數非必須

<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster" channelSendOptions="8">
<Manager className="org.apache.catalina.ha.session.DeltaManager" expireSessionsOnShutdown="false"
notifyListenersOnReplication="true"/>
<Channel className="org.apache.catalina.tribes.group.GroupChannel">
<Membership className="org.apache.catalina.tribes.membership.McastService"
address="228.10.0.4" port="45564" frequency="500" dropTime="3000"/> #多播地址應該是224~239,同一組裏的多播地址相同
<Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
address="192.168.2.197" port="4000" autoBind="100" selectorTimeout="5000" maxThreads="6"/> #address為本機能夠向外通信的地址
<Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">
<Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>
</Sender>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>
</Channel>
<Valve className="org.apache.catalina.ha.tcp.ReplicationValve" filter="/"/>
<Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/>
<ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
</Cluster>
2、編輯web.xml
vim /usr/local/tomcat/conf/web.xml 給應用程序加<distributable/>標簽,使其能夠實現復制。在web-app字段加 <distributable/>。
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<distributable/> #在配置文件的末尾第二行添加即可。
</web-app>
將web.xml放在對應的項目目錄下:
mkdir /usr/local/tomcat/webapps/test/WEB-INF
cp /usr/local/tomcat/conf/web.xml /usr/local/tomcat/webapps/test/WEB-INF/
重啟服務:catalina.sh stop
catalina.sh start
如果不能啟動,查看日誌logs/catalina.out信息如下(不能加入到組播) :blob.png
那麽添加到達組播的路由即可:route add -host 228.10.0.4 dev ens33

三、配置nginx負載均衡
vi /usr/local/nginx/conf/nginx.conf
upstream tomcat{
server 192.168.2.197:8080;
server 192.168.2.199:8080;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://tomcat;
}
}

nginx+Tomcat反向代理實現session會話保持