我们可以在 tomcat 的 server.xml 文件中包含文件来配置虚拟主机吗

Can we include files in the server.xml file of a tomcat to configure virtual host

我的 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 文件使用 include 选项来包含额外的主机条目,我可以将这些文件放在 tomcat.

的 conf 文件夹中

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

Here’s how to include a file in your Tomcat’s server.xml. Edit your server.xml, and at the very top of the file, right after any <?xml> declaration line (that’s optional), put the following DOCTYPE declaration to define a file entity:

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

This markup means that this document’s name is “server-xml”, and we’re defining a new entity named “connector1-config” which the XML parser can find in a file named “connector1-config.xml”. You can name your entities anything you want, as long as the parser accepts the characters you use. I suggest just using alpha-numeric characters and dash, to keep it simple. It turns out that if you don’t specify an absolute path to the file, the parser will look for the file in the same directory as the file that includes it, so the parser will look in Tomcat’s conf/ directory.

But, we haven’t yet used the connector XML entity we defined at the top of server.xml. At the point in the file where we want the parser to insert the connector’s XML, we need only to write “@connector1-config;” like this:

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

Then, in your connector1-config.xml file put the following XML snippet:

    <!-- 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" />

Using this include mechanism, you may include any file that resides in the same directory as Tomcat’s server.xml file.

If, instead, you want to include a file in another directory, where your Tomcat JVM user has read permission to the file, you can specify an absolute path when you define the entity, like this:

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

You use this entity the same way, just by placing “&connector1-config;” wherever you want the XML snippet included.

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

    <?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 的博客并支持他的工作。 我在这里引用了他的 post,因此即使博客被删除,社区也可以访问它。