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



我的 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中。您必须确保它们位于用户具有读取访问权限的位置。(不能包含用户没有读取访问权限的文件。

以下是在Tomcat的服务器中包含文件的方法.xml。 编辑您的 服务器.xml,以及文件的最顶部,紧跟在任何<?xml 之后> 声明行(可选),输入以下 DOCTYPE 用于定义文件实体的声明:

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

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

但是,我们尚未使用我们在 服务器顶部.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>

然后,在您的连接器 1-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的服务器.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的博客并支持他的工作。我在这里引用了他的帖子,因此即使博客被删除,社区也可以访问它。

最新更新