Jetty9 deployment method of multiple projects and virtual host configuration

1. Introduction

Jetty will automatically deploy almost all war packages, directories, and xml files under the webapps directory. If the war package, directory, and xml file have the same name, the deployment order is xml file> war package> directory. If there are: bank.war, bank directory, bank.xml in the webapps directory, where the bank directory is the decompressed directory of bank.war, bank.war package or bank directory is referenced in bank.xml, then only The xml file is deployed. If they have different names but are in the same project, the project will be deployed repeatedly! , So war packages, directories, and xml files of the same project should all have the same name.

2. Jetty context path (context path) configuration

  • Default context path
    By default, Jetty will use the project name in the webapps directory as the context path. If the project name is ROOT, the context path is /. If you place bank.war (or bank directory) under the webapps directory, the context path is / bank. If you rename bank.war to ROOT.war, the context path is /. Then the context path of the ULR to access the bank is: http: // localhost: 8888 / bank

  • Custom context path
    If the above method can not meet the requirements, you can configure the context path through the xml file:

  1. The first step: create an xml file in the webapps directory under the jetty root directory, the name can be arbitrary, such as jetyy-webapp.xml;
  2. Step 2: Suppose we have a bank.war project in the webapps directory. Let's configure a custom context path for it and add the following content in the xml file:
    jetyy-webapp.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_3.dtd">
<Configure id="GroceryWebapp" class="org.eclipse.jetty.webapp.WebAppContext">
  <Set name="contextPath">/bank888</Set>
  <Set name="war"><Property name="jetty.webapps" default="."/>/bank.war</Set>
</Configure>

After the configuration is complete, the context path to access the bank site is: http: // localhost: 8888 / bank888

3. Configure Virtual Host

Web hosting provides one more way to access the web. Web hosting is generally a domain name registered in DNS. Multiple domain names correspond to one IP. Virtual hosting can solve the same IP sharing a Jetty server instance.

The virtual host configuration has the following methods:

  • Use a copy of the xml file configured in the context path under the webapps directory, such as jetty-webapp.xml above.
  • For all contexts found in the webapps directory, create a custom deployment by binding and configuring the virtual host.
  • In the case of Jetty-embedded applications, virtual host configuration and access can also be achieved by directly calling the API
  • Use WEB-INF / jetty-web.xml to configure the virtual host ( obsolete )

3.1 The name of the virtual host

Jetty supports the following virtual host names in this form:

  • www.hostname.com, a fully qualified host name like this. Such a configuration can be received using access from www.hostname.com and hostname.com
  • .hostname.com, use wildcards to qualify the host, but only match any name of the first level, such as.foo.com can match www.foo.com and m.foo.com, but can not match www.other.foo.com
  • 10.0.0.2, the IP address can also be used as a virtual host name, which indicates that the context is to process requests received from the server port without the specified host name
  • @ConnectorName, the connection name, this is not strictly a virtual host, it only matches the request received by the connector whose name is set by Connector.setName (String).
  • www.√integral.com, non-ASCII and IDN domain names

3.2 Configure Virtual Host

We configure the virtual host for the context / bank888:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_3.dtd">
<Configure id="GroceryWebapp" class="org.eclipse.jetty.webapp.WebAppContext">
  <Set name="contextPath">/bank888</Set>
  <Set name="war"><Property name="jetty.webapps" default="."/>/bank.war</Set>
  <Set name="virtualHosts">
    <Array type="java.lang.String">
      <Item>www.MyVirtualDomain.com</Item>
      <Item>m.MyVirtualDomain.com</Item>
      <Item>*.OtherVirtualDomain.com</Item>
      <Item>@ConnectorName</Item>
      <Item>localhost</Item>
      <Item>127.0.0.1</Item>
    </Array>
  </Set>
</Configure>

Note: In practice, @ConnectorName is more prone to problems, and if it fails, the context will fail .
The access method becomes (virtual host + port + context path): http://127.0.0.1:8080/bank888
If you also need to add a virtual host for other items in webapps, you can continue in the above way under the webapps directory Create an xml file. It is recommended that the xml file is the same as the war name or directory name, otherwise it will cause repeated deployment.

Note: The way of virtual host only provides other ways to access the web, and does not affect the original access method (the original: IP + port + context path).

4. Close directory access under Jetty9 (close directory traversal and close file display in directory

  • Before closing the directory access, visit http: // localhost: 8888 to
    Insert picture description here
    display all the web under the webapps directory. The above situation occurs without configuring any custom context.
  • Customize the configuration web context, close directory access
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_3.dtd">
<Configure id="GroceryWebapp" class="org.eclipse.jetty.webapp.WebAppContext">
  <Set name="contextPath">/</Set>
  <Set name="war"><Property name="jetty.webapps" default="."/>/gro.war</Set>
  <Set name="virtualHosts">
    <Array type="String">
      <Item>www.MyVirtualDomain.com</Item>
      <Item>m.MyVirtualDomain.com</Item>
      <Item>*.OtherVirtualDomain.com</Item>
      <Item>localhost</Item>
      <Item>127.0.0.1</Item>
    </Array>
  </Set>
  <!--关闭目录访问-->
<Call name="setInitParameter">
	<Arg>org.eclipse.jetty.servlet.Default.dirAllowed</Arg>
	<Arg>false</Arg>
</Call>
</Configure> 

The web context defined above is /, and directory access is closed. If you visit http: // localhost: 8888 at this time:
Insert picture description here

Note: The practice results show that as long as there is a web context in the custom configuration context, it is basically impossible to display the web directory under webapps. It seems to have nothing to do with the following configuration:

<Call name="setInitParameter">
	<Arg>org.eclipse.jetty.servlet.Default.dirAllowed</Arg>
	<Arg>false</Arg>
</Call>

Scan the code to see more:
Insert picture description here

Published 381 original articles · praised 85 · 80,000 views +

Guess you like

Origin blog.csdn.net/weixin_40763897/article/details/105505113