windows系统下配置Nginx反向代理多tomcat共享session

Posted xddbky

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了windows系统下配置Nginx反向代理多tomcat共享session相关的知识,希望对你有一定的参考价值。

 

nginx、tomcat下载安装本篇不提了,直接说步骤:

1 配置nginx配置文件:

nginx下conf文件夹中的nginx.conf 添加下面代码,

  upstream web_apps
            server      127.0.0.1:8080 weight=1;
            server      127.0.0.1:8989 weight=1;
        
server 
            listen 80;
            server_name  localhost 127.0.0.1;

            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-Host $host;
            proxy_set_header X-Forwarded-Server $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Requested-For $remote_addr;
            proxy_set_header REMOTE-HOST $remote_addr;

            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";


            location / 
			root	html;
			index	index.html;
			 proxy_pass      http://web_apps;
			proxy_connect_timeout 600;
			proxy_read_timeout 600;
            

    

  

2配置tomcat 配置文件:

修改tomcat下conf中 server.xml

多个tomcat 中有3地方要不能冲突:

    <!-- 1-->
<Server port="19999" shutdown="SHUTDOWN"> 


    <!-- 2 -->
<Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443"/>

 <!-- 3 -->
 <Connector port="9999" protocol="AJP/1.3" redirectPort="8443" />

 port可以设大一点。

  然后每个加上tomcat自带的session同步 找到<Engine name="Catalina" defaultHost="localhost">关键句下面添加

<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.0.0.4"  
               port="45564"  
               frequency="500"  
               dropTime="3000"/>
   <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"  
             address="auto"  
             port="4000"  
             autoBind="100"  
             selectorTimeout="5000"  
             maxThreads="6"/>

   <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.MessageDispatchInterceptor"/>
         </Channel>

         <Valve className="org.apache.catalina.ha.tcp.ReplicationValve"  
                filter=""/>
         <Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/>

         <Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"  
                   tempDir="/tmp/war-temp/"  
                   deployDir="/tmp/war-deploy/"  
                   watchDir="/tmp/war-listen/"  
                   watchEnabled="false"/>

         <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
        </Cluster>
 

  然后把tomcat初始页码换一下方便测试效果,在apache-tomcat\\webapps\\ROOT 更改index.jsp内容为

<html>
        <head>
                <title> test</title>

        </head>

        <body>
        SessionID is <%=session.getId()%>
        <br/>

        SessionIP is <%=request.getServerName()%>
        <br/>

        SessionPort is <%=request.getServerPort()%>
		<!-- 端口号依次修改为:8080/8989-->
        <h1>tomcat_8080</h1>

</body>

</html>

  然后分别启动tomcat 和nginx 。

  直接输入localhost就如下图

技术图片

或者是这样:

技术图片

tomcat服务器会被nginx切换 但sessionID没有变。

另外说个nginx个操作指令 :cmd黑窗口进入nginx目录输入  nginx -s reload 会重新载入nginx的配置文件,不用重启nginx.

 如果出现异常先去看log文件 tomcat和nginx日志很详细。比如我遇到的:

19-Jun-2019 10:42:12.779 警告 [main] org.apache.catalina.startup.Catalina.load Unable to load server configuration from [D:\\apache-tomcat-9.0.17\\conf\\server.xml]
org.xml.sax.SAXParseException; systemId: file:/D:/apache-tomcat-9.0.17/conf/server.xml; lineNumber: 154; columnNumber: 113; Error at line [154] column [113]: [org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor]
at org.apache.tomcat.util.digester.Digester.createSAXException(Digester.java:1855)
at org.apache.tomcat.util.digester.Digester.createSAXException(Digester.java:1887)
at org.apache.tomcat.util.digester.Digester.startElement(Digester.java:1176)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(AbstractSAXParser.java:509)
at com.sun.org.apache.xerces.internal.parsers.AbstractXMLDocumentParser.emptyElement(AbstractXMLDocumentParser.java:182)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanStartElement(XMLDocumentFragmentScannerImpl.java:1339)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2784)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:602)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:505)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:842)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:771)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:643)
at org.apache.tomcat.util.digester.Digester.parse(Digester.java:1431)
at org.apache.catalina.startup.Catalina.load(Catalina.java:567)
at org.apache.catalina.startup.Catalina.load(Catalina.java:608)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.catalina.startup.Bootstrap.load(Bootstrap.java:306)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:491)
Caused by: java.lang.ClassNotFoundException: org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at org.apache.tomcat.util.digester.ObjectCreateRule.begin(ObjectCreateRule.java:115)
at org.apache.tomcat.util.digester.Digester.startElement(Digester.java:1173)
... 20 more

这个包已过时不在了,配上去肯定不行。。

参考博客:

https://www.cnblogs.com/beyang/p/9122406.html

https://blog.csdn.net/qq_23832313/article/details/83578813

over~

以上是关于windows系统下配置Nginx反向代理多tomcat共享session的主要内容,如果未能解决你的问题,请参考以下文章

Nginx在window环境下设置二级访问目录

Nginx反向代理+Tomcat服务

Windows下使用nginx搭建反向代理服务器

电信ADSL光纤下自建centos服务器 阿里云服务器反向代理 外面再加个CDN 该如何操作呢?

Windows下安装Nginx反向代理服务器

nginx正反向代理配置详解