【问题标题】:Can we include files in the server.xml file of a tomcat to configure virtual host我们可以在tomcat的server.xml文件中包含文件来配置虚拟主机吗
【发布时间】:2019-08-13 22:01:24
【问题描述】:

我的 tomcat 中有 server.xml 文件的以下部分。

<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>

<Host name="mydomain1"  appBase="d1"
    unpackWARs="true" autoDeploy="true">
    <Alias>xyz-mydomain1.com</Alias>
    <Alias>abc.mydomain1.com</Alias>


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

</Host>

<Host name="mydomain2"  appBase="d2"
    unpackWARs="true" autoDeploy="true">


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

</Host>

有什么方法可以通过使用包含选项的一些 xml 文件来包含额外的主机条目,这些 xml 文件可以放在我的 tomcat 的 conf 文件夹中。

【问题讨论】:

    标签: virtualhost tomcat8 server.xml


    【解决方案1】:

    我不确定这是否是您要查找的内容,但您可以通过 XML 实体包含在您的 tomcat XML 中包含文件。您必须确保它们位于用户具有读取权限的位置。 (您不能包含用户没有读取权限的文件。

    以下是在 Tomcat 的 server.xml 中包含文件的方法。编辑您的 server.xml,在文件的最顶部,任何 之后 声明行(这是可选的),放入以下 ​​DOCTYPE 定义文件实体的声明:

        <?xml version='1.0' encoding='utf-8'?>
        <!DOCTYPE server-xml [
          <!ENTITY connector1-config SYSTEM "connector1-config.xml">
        ]>
    

    这个标记意味着这个文档的名字是“server-xml”,我们是 定义一个名为“connector1-config”的新实体,XML 解析器 可以在名为“connector1-config.xml”的文件中找到。你可以命名你的 实体任何你想要的,只要解析器接受 你使用的字符。我建议只使用字母数字字符和 破折号,保持简单。事实证明,如果你不指定 文件的绝对路径,解析器将在 与包含它的文件相同的目录,因此解析器将查找 在 Tomcat 的 conf/ 目录中。

    但是,我们还没有使用我们在 server.xml 的顶部。在文件中我们想要解析器的位置 要插入连接器的 XML,我们只需要编写 “@connector1-配置;”像这样:

    <Server ...>
        <Service ...>
     
            <!-- See conf/connector1-config.xml for this <a href="https://www.mulesoft.com/exchange#!/?types=connector" target="_blank" rel="" title="Cloud Connectors" >connector's</a> config. -->
            &connector1-config;
     
        </Service>
    </Server>
    

    然后,在您的 connector1-config.xml 文件中放入以下 ​​XML sn-p:

        <!-- Define a non-SSL Coyote HTTP/1.1 <a href="https://www.mulesoft.com/exchange#!/?types=connector" target="_blank" rel="" title="Cloud Connectors" >Connector</a> on port 8089 -->
        <Connector port="8089" protocol="HTTP/1.1"
                   connectionTimeout="20000"
                   redirectPort="8443" />
    

    使用此包含机制,您可以包含驻留在 与 Tomcat 的 server.xml 文件相同的目录。

    如果您想在另一个目录中包含一个文件,其中 您的 Tomcat JVM 用户对该文件具有读取权限,您可以指定 定义实体时的绝对路径,如下所示:

    <?xml version='1.0' encoding='utf-8'?>
    <!DOCTYPE server-xml [
      <!ENTITY connector1-config SYSTEM "file:///opt/myproject/connector1-config.xml">
    ]>
    

    你以同样的方式使用这个实体,只是放置 “&connector1-config;”任何你想包含 XML sn-p 的地方。

    要包含多个文件,请尝试以下操作:

        <?xml version='1.0' encoding='utf-8'?>
        <!DOCTYPE server-xml [
            <!ENTITY file1 SYSTEM "file1.xml">
            <!ENTITY file2 SYSTEM "file2.xml">
        ]>
    

    然后在文件中您希望解析器插入相应文件代码的位置尝试以下操作:

    <Server ...>
        <Service ...>
     
            <!-- See conf/file1.xml for this -->
            &file1;
            <!-- See conf/file2.xml for this -->
            &file2;
     
        </Service>
    </Server>
    

    我的回答主要是基于这个post by jasonb请访问 jasonb 的博客并支持他的工作。我在这里引用了他的帖子,因此即使博客被删除,社区也可以访问它。

    【讨论】:

    • 我可以像这样包含多个文件吗?
    • @GeoThomas 我在回答中包含了一个关于如何包含多个文件的部分。
    猜你喜欢
    • 2013-03-28
    • 2014-01-31
    • 2021-06-24
    • 2016-08-22
    • 2013-01-30
    • 1970-01-01
    • 1970-01-01
    • 2015-06-29
    • 1970-01-01
    相关资源
    最近更新 更多