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

偶尔善良 提交于 2021-01-28 07:03:34

问题


I have the following section of the server.xml file in my 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>

Is there any way I could include the extra host entries by using an include option to some xml files that I can place in the conf folder of my tomcat.


回答1:


I am not sure if this is what you are looking for, but you can include files in your tomcat XML via XML entity includes. You must make sure though that they are in a location where the user has read access. (You can not include files that the user has not read access to.)

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 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 -->
    <<a href="https://www.mulesoft.com/exchange#!/?types=connector" target="_blank" rel="" title="Cloud Connectors" >Connector</a> 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.

To include multiple files try the following:

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

and then at the point in the file where you want the parser to insert the respective file code try this:

<Server ...>
    <Service ...>

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

    </Service>
</Server>

My answer is mostly based on this post by jasonb. Please visit jasonb's blog and support his work. I quoted his post here, so it is accessible for the community even if the blog were to be deleted.



来源:https://stackoverflow.com/questions/57485833/can-we-include-files-in-the-server-xml-file-of-a-tomcat-to-configure-virtual-hos

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!