首页 » 架构配置 » Tomcat » 正文

tomcat 配置虚拟主使用nginx 代理无法访问

同事公司要求,在一台服务器上的一个tomcat 实例下部署多个虚拟主机,然后在前端使用NG 反向代理,结果出现,使用两个域名去访问,都是将请求转发到默认的第一个虚拟主机上:

同事的配置为:

[[email protected] /home/tomcat-admin 10:32:51]#cat conf/server.xml
<?xml version='1.0' encoding='utf-8'?>

<Server port="8005" shutdown="SHUTDOWN">
  <Listener className="org.apache.catalina.startup.VersionLoggerListener" />

    <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />

  <Listener className="org.apache.catalina.core.JasperListener" />

  <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="800" 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="aaa.1111.com"  appBase="/home/tomcat-admin/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 "%r" %s %b" />

      </Host>

  <!--  第二个虚拟主机 -->
   <Host name="aaa.2222.com"  appBase="/home/tomcat-admin/999"

            unpackWARs="true" autoDeploy="true">

        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="999." suffix=".txt"
               pattern="%h %l %u %t "%r" %s %b" />

      </Host>

    </Engine>
  </Service>
</Server>

Tomcat 结构:

第一个虚拟机主的根目录:

[[email protected] /home/tomcat-admin 10:40:20]#ls  /home/tomcat-admin/webapps/ROOT
META-INF  static  WEB-INF

第二个虚拟主机的根目录:

[[email protected] /home/tomcat-admin 10:42:35]#ls /home/tomcat-admin/999/ROOT
META-INF  static  WEB-INF

换做我在测试的时候结果报404:

解决方法:

修改tomcat 配置,第一个虚拟主机配置不懂,修改第二个虚拟主机的配置为:

[[email protected] /home/tomcat-admin 10:32:51]#cat conf/server.xml
<?xml version='1.0' encoding='utf-8'?>

<Server port="8005" shutdown="SHUTDOWN">
  <Listener className="org.apache.catalina.startup.VersionLoggerListener" />

    <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />

  <Listener className="org.apache.catalina.core.JasperListener" />

  <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="800" 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="aaa.1111.com"  appBase="/home/tomcat-admin/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 "%r" %s %b" />

      </Host>

  <!--  第二个虚拟主机 -->
   <Host name="aaa.2222.com"  appBase="webpps"

            unpackWARs="true" autoDeploy="true">
     <Context path="" docBase="/home/tomcat-admin/999/ROOT" debug="0" reloadable="true" crossContext="true"/>

        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="999." suffix=".txt"
               pattern="%h %l %u %t "%r" %s %b" />

      </Host>

    </Engine>
  </Service>
</Server>

重启tomcat,测试访问OK!

说明:在配置TM 虚拟主机使用NG 代理的时候!!

赞 (0)

发表评论