Detail tomcat setting the default path causes the project url conflict resolution

  • 2021-01-03 21:09:56
  • OfStack

preface

tomcat is excellent as an java container, but there will still be a few holes to avoid. Note 1 here.

START

The problem

url path conflicts after multiple projects are deployed

Scene description

1. There are two projects under webapps, projectA and projectB. Except for the management information interface, the other two projects have security verification mechanism.

2.projectA since the front end and back end are not separated, static resources are also stored in the java project. When making interface requests in static resources, the package name is not written, such as login, the js code will splicip + port + url(/login) which is currently set, but not before /login, so there is no problem in the local test, only when the deployment of such a problem. That's another problem. Let's do it.

3.projectB is a working project

Solution for Scenario 2

Open the configuration file in tomcat at < Host > Add it to the tag < Context > Set as the server's default access path so that package names are avoided, but this method is highly irregular and is not recommended.


<Host name="localhost" appBase="webapps"
      unpackWARs="true" autoDeploy="true">

    <!-- SingleSignOn valve, share authentication between web applications
       Documentation at: /docs/config/valve.html -->
    <!--
    <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
    -->

    <!-- Access log processes all example.
       Documentation at: /docs/config/valve.html
       Note: The pattern used is equivalent to using pattern="common" -->
    <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" />
    <Context path="" docBase="/usr/tomcat8.6/webapps/sc_edu" debug="0" reloadable="true"/>

  </Host>

Code interpretation


<Context path="" docBase="/usr/tomcat8.6/webapps/sc_edu" debug="0" reloadable="true"/>
path and doBase1 represent the specified packet path and can be uninstalled directly in docBase for simplicity. Restart tomcat, test ip+port can access the resources in this package directly, however, when accessing resources in other packages, url ambiguity will appear. Originally wanted to access projectB, but mapped to the projectA project. Only part of url has this problem.

Then our solution is to install another tomcat and only deploy projects that require direct path-mapping

This returns to tomcat and copies the cp command


$> cp -r tomcat8.5/ tomcat8.6/

Then move projectA from tomcat8.5 to tomcat8.6.

Delete in tomcat8.5


<Context path="" docBase="/usr/tomcat8.6/webapps/sc_edu" debug="0" reloadable="true"/>

The following changes to ES90en.xml are required in tomcat8.6.

Change shutdown to port 8006, as long as it is different from tomcat8.5 and the port does not conflict.


<Server port="8006" shutdown="SHUTDOWN">

Change the port of the request, same as above


<Connector port="8081" protocol="HTTP/1.1"
        connectionTimeout="20000"
        redirectPort="8443" />

< Host > The content in the tag is copied over and needs to be modified to the corresponding mapping path.

In this way, both tomcat can be run at the same time, starting and closing do not affect each other.


Related articles: