解决 Tomcat 无法绑定 80 端口的问题,以及 Tomcat 配置虚拟目录、二级域名等

问题

今天安装完 Tomcat,安装时把 Tomcat 默认的 HTTP/1.1 Connector Port 从 8080 改为了 7080,启动 Tomcat,在浏览器中输入 Http://localhost:7080/ 也能看到 Tomcat 的欢迎页。后来想改成 80 端口,然后想测试绑定一个域名看看,于是进入 C:\Program Files\Apache Software Foundation\Tomcat 7.0\conf 的 server.xml,把配置改成下面的代码:

(注意:server.xml 中不能用中文注释,否则 Tomcat 启动会失败!)

<?xml version='1.0' encoding='utf-8'?>
<Server port="7005" shutdown="SHUTDOWN">
  <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">
    <!-- 下面这里的 port 从 7080 改为 80 -->
    <Connector port="80" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    <Connector port="7009" protocol="AJP/1.3" redirectPort="8443" />
    <!-- 下面这里的 defaultHost 从 localhost 改为 www.testMyTomcat.com -->
    <Engine name="Catalina" defaultHost="www.testMyTomcat.com">
      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
               resourceName="UserDatabase"/>
      </Realm>
      <Host name="www.testMyTomcat.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 &quot;%r&quot; %s %b" />

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

注意:server.xml 中不能用中文注释,否则 Tomcat 启动会失败,Tomcat 里面的 log 文件夹中的日志会提示诸如下面的错误消息:

警告: Catalina.start using conf/server.xml: 
com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: 2 字节的 UTF-8 序列的字节 2 无效。
    at com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.invalidByte(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.read(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.load(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.scanData(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLScanner.scanComment(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanComment(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
    at org.apache.tomcat.util.digester.Digester.parse(Digester.java:1537)
    at org.apache.catalina.startup.Catalina.load(Catalina.java:617)
    at org.apache.catalina.startup.Catalina.load(Catalina.java:665)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.apache.catalina.startup.Bootstrap.load(Bootstrap.java:281)
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:455)

 

上面的代码,共修改了 2 个地方:

1. Connector 节点的 port 从 7080 改为 80

2. Engine 节点的 defaultHost 从 localhost 改为 www.testMyTomcat.com

修改完后,重启 Tomcat,然后配置 C:\Windows\System32\Drivers\etc 的 Hosts 文件,增加一个映射:

127.0.0.1    www.testMyTomcat.com

在浏览器中输入 http://www.testMyTomcat.com 却无法显示,就像随意在浏览器中输入一个错误的地址后,显示的一样的错误页面。左思右想,却怎么也不知道到底是哪里错了。后来咨询同事,才想到可能是 80 端口被占用了,看看是不是被 IIS 给占用了。打开 IIS, 发现:

这就是当时做 Cookie 跨域名传递测试时配置的,原来如此,把 80 改为其它的后,再次打开浏览器,输入 http://www.testMyTomcat.com 后终于成功了,哈哈,心里一下子就开心了。

配置虚拟目录

配置完域名配置,那么如何配置虚拟目录呢?前面的配置不变,把 Host 节点替换成这样:

<Host name="www.testMyTomcat.com"  appBase="webapps"
    unpackWARs="true" autoDeploy="true">
<Alias>testMyTomcat.com</Alias>
<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="E:\Deployment_Projects\Tomcat_WebApp\cas" path="sso"  reloadable="true"/>
</Host>

注意到了吗?只是在 </Host> 前面增加了一个

<Context docBase="E:\Deployment_Projects\Tomcat_WebApp\cas" path="sso"  reloadable="true"/>

其中 docBase 属性表示虚拟目录的地址,默认我的 tomcat 是安装在 C:\Program Files\Apache Software Foundation\Tomcat 7.0 的,我不想把 cas 项目也放在 C 盘,所以指定了一个绝对路径,path 属性表示 URL 访问时的路径。

修改完后重新启动 Tomcat,在浏览器中输入 http://www.testmytomcat.com/sso,结果成功!

解决 Tomcat 无法绑定 80 端口的问题,以及 Tomcat 配置虚拟目录、二级域名等_第1张图片

配置二级域名

如果我想把 cas 项目(注:cas 是 java 里面著名的单点登录 SSO 项目)单独做一个二级域名,而不是以虚拟目录的形式提供给外部,那么该怎样配置呢?

1. 首先依旧配置 C:\Windows\System32\Drivers\etc 的 Hosts 文件,增加一个映射:

127.0.0.1    cas.testMyTomcat.com

注:cas.testMyTomcat.com 是我随意起的,你也可以起名为 sso.testMyTomcat.com ,貌似这样大气一点,呵呵...

2. 修改 C:\Program Files\Apache Software Foundation\Tomcat 7.0\conf 的 server.xml,把配置改成下面的代码(PS:依旧只是把玩 Host 节点):

<Host name="www.testMyTomcat.com"  appBase="webapps"
    unpackWARs="true" autoDeploy="true">
    <Alias>testMyTomcat.com</Alias>
    <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" />
</Host>

<Host name="cas.testMyTomcat.com"  appBase="E:\Deployment_Projects\Tomcat_WebApp"
    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="E:\Deployment_Projects\Tomcat_WebApp\cas" path=""  reloadable="true"/>
</Host>

请注意第二个 Host 节点的 appBase 不能和第一个 Host 节点的 appBase 相同。

修改完后重新启动 Tomcat,在浏览器中输入 http://cas.testmytomcat.com/,结果成功!

完整配置如下:

<?xml version='1.0' encoding='utf-8'?>
<Server port="7005" shutdown="SHUTDOWN">
  <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="80" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    <Connector port="7009" protocol="AJP/1.3" redirectPort="8443" />
    <Engine name="Catalina" defaultHost="www.testMyTomcat.com">
      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
               resourceName="UserDatabase"/>
      </Realm>

      <Host name="www.testMyTomcat.com"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">
        <Alias>testMyTomcat.com</Alias>
        <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" />
      </Host>

      <Host name="cas.testMyTomcat.com"  appBase="E:\Deployment_Projects\Tomcat_WebApp"
            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="E:\Deployment_Projects\Tomcat_WebApp\cas" path=""  reloadable="true"/>
      </Host>
    </Engine>
  </Service>
</Server>

 

多端口配置

如果我想把 cas 项目只是单独一个端口,那又该如何配置呢?

  <Service name="Catalina">
    <Connector port="80" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
    <Connector port="7009" 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" />
      </Host>
    </Engine>
  </Service>

  <Service name="Catalina2">
    <Connector port="85" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="86" />
    <Connector port="87" protocol="AJP/1.3" redirectPort="86" />
    <Engine name="Catalina2" defaultHost="localhost">
      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/>
      </Realm>

      <Host name="localhost"  appBase="E:\Deployment_Projects\Tomcat_WebApp"
            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="E:\Deployment_Projects\Tomcat_WebApp\cas" path=""  reloadable="true"/>
      </Host>
    </Engine>
  </Service>

注:首先复制一个 Service 节点及子节点,修改相关的 name 属性和端口号。

打开浏览器,分别输入 Http://localhost/   和  Http://localhost:85/, 测试通过!

解决 Tomcat 无法绑定 80 端口的问题,以及 Tomcat 配置虚拟目录、二级域名等_第2张图片

谢谢浏览!

你可能感兴趣的:(解决 Tomcat 无法绑定 80 端口的问题,以及 Tomcat 配置虚拟目录、二级域名等)