您的位置:首页 > 运维架构 > Tomcat

tomcat设置不需要项目名便可访问项目(直接用域名或者ip和端口访问)

2016-10-14 17:13 716 查看
   实际生产中往往访问web项目要求直接使用ip+端口或者使用域名便可直接访问项目,不加/项目名称。配置起来其实是非常简单的。

在tomcat\conf目录下找到server.xml,在<Host></Host>配置里面添加一行配置

<Context docBase="D:\apache-tomcat-8.0.36-windows-x64\apache-tomcat-8.0.36\webapps\waGong" path="/" reloadable="true" />
docBase是项目的物理路径(项目不必非要放在tomcat下面,可以放在磁盘的任意位置,只要此处物理路径配置正确,tomcat能找到项目位置就可以),path是虚拟路径。将path设置成/便可不需要项目名称便可访问项目。

<Host></Host>里面可以设置多个项目,每个项目一行Context设置。

配置完成后打开浏览器访问下http://localhost:80 效果如下



项目上线的时候,将Host的name属性从默认的localhost修改为域名。项目便可直接通过域名访问。

server.xml

<?xml version="1.0" encoding="UTF-8"?>
<Server port="8005" shutdown="SHUTDOWN">
  <Listener className="org.apache.catalina.startup.VersionLoggerListener"/>
  <Listener SSLEngine="on" className="org.apache.catalina.core.AprLifecycleListener"/>
  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener"/>
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"/>
  <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener"/>
    <Resource auth="Container" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" name="UserDatabase" pathname="conf/tomcat-users.xml" type="org.apache.catalina.UserDatabase"/>
  </GlobalNamingResources>
  <Service name="Catalina">

    <Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>
    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443"/>
    <Engine defaultHost="localhost" name="Catalina">
      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/>
      </Realm>

      <Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true">
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" pattern="%h %l %u %t "%r" %s %b" prefix="localhost_access_log" suffix=".txt"/>

         <Context docBase="D:\apache-tomcat-8.0.36-windows-x64\apache-tomcat-8.0.36\wtpwebapps\weike" path="/weike" reloadable="true" source="org.eclipse.jst.jee.server:weike"/>

        <Context docBase="D:\apache-tomcat-8.0.36-windows-x64\apache-tomcat-8.0.36\webapps\waGong" path="/" reloadable="true" />
      </Host>
    </Engine>
  </Service>
</Server>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息