Redirecting requests between contexts in Tomcat

I would like to be able to forward the cross context query to Tomcat using the Tuckey URLRewrite filter. For example, I would like to be able to route an incoming request using an SEO / friendly user URL such as http://example.com/group-elements/300245/some-descriptive-text , where "group-elements" is not the name of the deployed application, but the URL mapped to the Java Spring controller for the "foo" application, for example http://example.com/foo/app/group/300245/elements . I am using Tomcat 7.0.27 and URLRewrite 3.2.0; I am working with Java Spring 3.1 web applications.

The URLRewrite 3.20 documentation specifies the optional "context" attribute for the "to" filter parameter element:

If your application server is configured to allow "cross context" communication then this attribute can be used to forward (and only forward, not redirect or other "to" types) requests to a named servlet context. On Tomcat, for instance, the application contexts in the server configuration (server.xml or context.xml) need the option crossContext="true". For instance, the two applications mentioned before ("app" and "forum") have to be defined as: <Context docBase="app" path="/app" reloadable="true" crossContext="true"/> <Context docBase="forum" path="/forum" reloadable="true" crossContext="true"/> 

Given the original discussion about this function , the “context” attribute seems to be what I'm looking for. However, I was not able to correctly enable the cross-request forwarding request.

Here is my "Context" "foo" application in conf / server.xml:

 <Context docBase="foo" path="/foo" reloadable="true" crossContext="true"/> 

I have a urlrewrite.xml file and a web.xml file in webapps / ROOT / WEB-INF /. Here's what they look like:

urlrewrite.xml:

 <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 3.2//EN" "http://tuckey.org/res/dtds/urlrewrite3.2.dtd"> <urlrewrite> <rule> <from>baz</from> <!-- Note: this 'to' element value has an error. See the edit at bottom of this post for corrected version. --> <to context="foo">/foo/app/group/300245/elements</to> </rule> </urlrewrite> 

web.xml:

 <?xml version="1.0" encoding="ISO-8859-1"?> <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_5.xsd" version="2.5"> <filter> <filter-name>UrlRewriteFilter</filter-name> <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class> <init-param> <param-name>logLevel</param-name> <param-value>WARN</param-value> </init-param> </filter> <filter-mapping> <filter-name>UrlRewriteFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app> 

The rule defined above in urlrewrite.xml is intentionally basic and hard-coded. In this case, I'm just trying to get the cross-contextual aspect of a rule that works before developing regular expressions in 'to' and 'from'.

When I request http://example.com/baz with this rule, Tomcat returns error 404: "The requested resource (/ baz) is unavailable." I tried several options in the "to" filter parameter, but so far nothing has worked. And I could not find examples of how to use the "context".

Any ideas on how I could get this filtering of requests by cross-context request? Is it possible? I suppose I can achieve what I am trying to do by renaming foo.war to ROOT.war or changing the root application as mentioned here , I would like to try to do this using URLRewrite if this is not feasible or a bad idea on the face .

If you show more of my configuration, please let me know. Thanks in advance for any input.

Edit

Thanks to Christopher Schulz for the helpful answer. In my case, the problem was caused by two things: 1) the absence of the context.xml file in webapps / ROOT / META-INF and 2) the error in the 'to' element in the URL rewrite rule in webapps / ROOT / WEB-INF / urlrewrite.xml .

The fix is ​​related to the placement of the corresponding context.xml file in webapps / ROOT / META-INF. For reference, for anyone facing this problem, this file is as follows:

WebApps / ROOT / META-INF / context.xml

 <?xml version='1.0' encoding='utf-8'?> <Context docBase="ROOT" path="/" reloadable="true" crossContext="true" /> 

As Schultz mentions, this is only necessary for the context with crossContext = "true", which must be defined for the context implied in the "from" element in the specified URL rewrite rule (here is this ROOT). There is no need to explicitly define the context for the application in the URL rewrite rule. In other words, you do not need to manually create the context.xml file for this application - therefore, continuing with the above example, you do not need to manually define and place the context.xml file in webapps / foo / META-INF /.

Schulz’s response reflects contextual guidelines in the official Tomcat documentation: http://tomcat.apache.org/tomcat-7.0-doc/config/context.html#Defining_a_context .

The problem was also caused by the fact that the URL rewrite rule in my original post had an error. The correct version should have been:

urlrewrite.xml:

 <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 3.2//EN" "http://tuckey.org/res/dtds/urlrewrite3.2.dtd"> <urlrewrite> <rule> <from>baz</from> <!-- Note: the use of '/app' instead of '/foo/app/' below --> <to context="foo">/app/group/300245/elements</to> </rule> </urlrewrite> 
+4
source share
2 answers

If your (real) webapp is deployed to /foo and you want URLs like /group-elements/baz to be rewritten on the fly to redirect ( not ) to /foo/app/group/300245/elements , then You will have to deploy the rewrite filter in one of two places: /group-elements or / .

The above configuration seems to be deployed to ROOT (which is / ), but then maps the URL /baz to /foo/app/group/300245/elements . Instead, you probably want to:

 <rule> <from>/group-elements/baz</from> <to context="foo">/foo/app/group/300245/elements</to> </rule> 

It looks like you were trying to click on http://example.com/baz , which I would expect to work. The last bit of magic will be the cross-context of the ROOT context (note that your webapp should NOT be cross-context: only for urlrewrite). You can change the ROOT as a cross context using addint crossContext="true" to webapps/ROOT/META-INF/context.xml .

Finally, you really have to stop putting <Context> elements in server.xml: leaving them there basically means that you need to restart Tomcat to change your Webapp deployment.

+3
source

After commenting, I come to this possible conclusion:

I think you mix the concepts of reverse proxy with cross context. Cross context is a method of exchanging data between two web applications on the same application server. A reverse proxy, such as Apache http, can rewrite a URL to pass it to a specific server behind it, effectively hiding any unwanted parts or performing other operations, such as load balancing.

The infrastructure will be: client → reverse proxy → application server

+1
source

All Articles