首页 > 解决方案 > 我们可以在tomcat的server.xml文件中包含文件来配置虚拟主机吗

问题描述

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

<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文件夹中。

标签: virtualhosttomcat8server.xml

解决方案


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

下面介绍如何在 Tomcat 的 server.xml 中包含文件。编辑您的 server.xml,并在文件的最顶部,任何 <?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 实体。在我们希望解析器插入连接器 XML 的文件中,我们只需要编写“@connector1-config;” 像这样:

<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 片段:

    <!-- 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 片段。

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

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

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


推荐阅读