Tomcat实现多域名访问详解

2019-10-18 19:48:16丽君

这就是我们的Tomcat默认绑定,我们可以通过localhost直接访问项目即是这个配置。下面我们配一个通过域名来访问项目的配置,在Engine标签下我们在添加一个Host配置:

<!--www.hpugs.com-->
    <Host name="www.hpugs.com" 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 "%r" %s %b" /> 
      <Context docBase="C:Program Filesapache-tomcat-8.5.13webappspc-server" path="" reloadable="true" />
    </Host>

注意:Context 标签必须放置于Value下,不然Tomcat启动将会报错,这里解释两个参数:docBase项目实际路径;path项目访问虚拟路径。简单的说docBase指向我们的项目具体位置,path为我们访问路径。

  6、如何进行多域名绑定

  很简单如上,在Engine标签下我们再添加几个Host配置即可

<Engine name="Catalina" defaultHost="localhost">

    <Realm className="org.apache.catalina.realm.LockOutRealm">
      <!-- This Realm uses the UserDatabase configured in the global JNDI
        resources under the key "UserDatabase". Any edits
        that are performed against this UserDatabase are immediately
        available for use by the Realm. -->
      <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
        resourceName="UserDatabase"/>
    </Realm>
    
    <!--localhost-->
    <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 "%r" %s %b" />
    </Host>

    <!--www.hpugs.com-->
    <Host name="www.hpugs.com" 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 "%r" %s %b" /> 
      <Context docBase="C:Program Filesapache-tomcat-8.5.13webappspc-server" path="" reloadable="true" />
    </Host>
    
    <!--m.hpugs.com-->
    <Host name="m.hpugs.com" 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 "%r" %s %b" /> 
      <Context docBase="C:Program Filesapache-tomcat-8.5.13webappsweb-mobile-server" path="" reloadable="true" />
    </Host>
  </Engine>

7、最后需要说几点:

  defaultHost是指默认Host配置,当访问域名没有进行绑定时,使用默认Host配置

  Engine 标签下默认localhost配置,是为了没有进行域名项目绑定的域名,通过域名+项目名称来访问。