开发者

Tomcat: two context paths for one webapp

The problem is: I have a web app and this web app is deployed to the $TOMCAT_HOME/webapps/XXX directory. I can reach that on the http://localhost:8080/XXX address BUT, I would like to reach the web app on the http://localhost:8080/YYY address too. I added the following to the server.xml:

<Server>
    <Service>
        <Engine>
            <Host>
                .......
               开发者_如何学Python <Context path="/YYY" docBase="XXX"></Context>
            </Host>
        </Engine>
    </Service>
</Server>

It helped but the Tomcat started two web contexts and it caused some other problem. Is it possible to create a "multiple" address for one web app?


The url of web application is assebled as follows:

PROTOCOL://DOMAIN:PORT/CONTEXT/pagename

The solutions for having same app on two distinct address are as follows:

  1. If you want to differ only in protocol (let's say between http, and https) then just have 2 connectors in server.xml.

  2. if you want to differ in DOMAIN name, then this is solved on DNS level.

  3. If you want to differ in context name (web application name), you should put apache in front (mod_proxy or mod_ajp) and then create a rewrite rule (mod_rewrite). let's say rewrite all from /a/* and /b/* to /c/*

  4. If you want to differ in page name, you should use servlet mappings.

Putting apache in front of tomcat via mod_proxy is very easy, there are multiple resources on the web. Very bad would be to duplicate applications (have everything loaded twice).

As for your question, i would advise agains duplication in server.xml.

<Context docBase="myapp" path="/address1" reloadable="true" />
<Context docBase="myapp" path="/address2" reloadable="true" />

This is killer for memory, as well as for session mechanisms, concurency, etc.


Try using Tomcat's Rewrite Valve (docs here)

{TOMCAT_HOME}/conf/server.xml

<Host name="localhost" ... >
    <Valve className="org.apache.catalina.valves.rewrite.RewriteValve" />
</Host>

{TOMCAT_HOME}/conf/Catalina/localhost/rewrite.config

RewriteCond %{REQUEST_URI} ^/XXX/.*$
RewriteRule ^/XXX/(.*)$ /YYY/$1 [L]

Note this security warning in the Tomcat 9 docs (here),

Security warning: Due to the way Java's regex matching is done, poorly formed regex patterns are vulnerable to "catastrophic backtracking", also known as "regular expression denial of service" or ReDoS. Therefore, extra caution should be used for RewriteRule patterns. In general it is difficult to automatically detect such vulnerable regex, and so a good defense is to read a bit on the subject of catastrophic backtracking. A good reference is the OWASP ReDoS guide.


Try using the crossContext attribute:

<Context path="/YYY" docBase="XXX" crossContext="true"></Context>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜