一个Tomcat下运行多个项目,在不同端口

1.背景描述:

  • 你希望在同一个tomcat服务器中运行多个不同的项目,而两个项目却又在不同的端口下面,考虑下面的解决方法。

2.方法
设置配置文件server.xml在配置文件中的service部分,每一个service代表着一个应用目录,可以更改端口那种,若是不需要在不同的端口下面运行,实际上有更简单的方法
直接在Service下的Host的下面的Engine里,增加一个新的Context即可,可以在这里面配置相关路径

<Context docBase="../webapps1/vd/" path="" reloadable="true"></Context>

其中的doBase表示项目的地址,path表示项目的访问路径,这里使用空字符,表示根目录下面,无需加上项目名。

  • 1.想要在不同的端口下运行,首先,在tomcat根目录下面新增一个webapps1,将新的项目放在该目录下
    在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

  • 2.然后,只需要新增一个service即可,下面的代码已经去掉所有注释,且将新增的项目配置好
<?xml version="1.0" encoding="UTF-8"?>
  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
  <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
  <GlobalNamingResources>
    <Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
              description="User database that can be updated and saved"
              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
              pathname="conf/tomcat-users.xml" />
  </GlobalNamingResources>


  <Service name="Catalina">
    <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
    <Engine name="Catalina" defaultHost="localhost">
      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
               resourceName="UserDatabase"/>
      </Realm>
      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log" suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />
	<Context docBase="../webapps/urls" path="" reloadable="true"></Context>
      </Host>
    </Engine>
  </Service>



  <!--这是一个新的端口下运行的项目,新的名字与上面有区别就行,这里为Catalina1-->
  <Service name="Catalina1">
  <!--这里更改了新的端口为80-->
    <Connector port="80" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
    <!--这里需要将name改为上面建立的名字,要一致-->
    <Engine name="Catalina1" defaultHost="localhost">
      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
               resourceName="UserDatabase"/>
      </Realm>
      <!--这里需要将appBase改为第一步中建立的目录名,也就是上面的webapps1-->
      <Host name="localhost"  appBase="webapps1"
            unpackWARs="true" autoDeploy="true">
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log" suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />
      <!--这里是新的项目的地址-->
	<Context docBase="../webapps1/vd/" path="" reloadable="true"></Context>
      </Host>
    </Engine>
  </Service>
  
</Server>

猜你喜欢

转载自blog.csdn.net/qq_44625080/article/details/105893314