在Tomcat中的上下文之间转发请求 [英] Forwarding requests between contexts in Tomcat

查看:829
本文介绍了在Tomcat中的上下文之间转发请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够使用Tuckey URLRewrite过滤器在Tomcat中进行跨上下文请求转发.例如,我希望能够使用SEO//用户友好的URL(例如 http://example.com/group-elements/300245/some-descriptive-text ,其中"group-elements"不是已部署应用程序的名称映射到应用程序"foo"的Java Spring控制器方法,例如 http://example. com/foo/app/group/300245/elements .我正在使用Tomcat 7.0.27和URLRewrite 3.2.0;我正在使用Java Spring 3.1 Web应用程序.

I'd like to be able to do cross-context request forwarding in Tomcat with the Tuckey URLRewrite filter. For example, I'd like to be able to route an incoming request with an SEO-/user-friendly URL like http://example.com/group-elements/300245/some-descriptive-text, where "group-elements" isn't the name of a deployed application, to a URL mapped to a Java Spring controller method for application 'foo', like http://example.com/foo/app/group/300245/elements. I'm using Tomcat 7.0.27 and URLRewrite 3.2.0; I'm working with Java Spring 3.1 web applications.

URLRewrite 3.20文档指出了至"过滤器参数元素的可选上下文"属性:

The URLRewrite 3.20 documentation notes an 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 that and the original discussion about the feature, the 'context' attribute seems to be what I'm looking for. However, I haven't been able to properly enable cross-context request forwarding.

这是我在conf/server.xml中的上下文"条目应用程序"foo":

Here's my 'Context' entry application 'foo' in conf/server.xml:

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

我在webapps/ROOT/WEB-INF/中有urlrewrite.xml文件和web.xml文件.它们是这样的:

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

urlrewrite.xml:

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's 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>

上面在urlrewrite.xml中定义的规则是故意的并且是硬编码的.在这种情况下,我只是试图在开发"to"和"from"中的正则表达式之前使规则的跨上下文方面起作用.

The rule defined above in urlrewrite.xml is intentionally basic and hard-coded. In this case I'm simply trying to get the cross-context aspect of the rule working before developing the regular expressions in 'to' and 'from'.

当我请求 http://example.com/baz 并使用该规则时,Tomcat返回一个404错误,提示请求的资源(/baz)不可用."我尝试了"to"过滤器参数的一些变体,但是什么都没起作用.而且我还找不到任何有关如何使用上下文"的示例.

When I request http://example.com/baz with that rule in place, Tomcat returns a 404 error saying "The requested resource (/baz) is not available." I've tried a few variations in the 'to' filter parameter, but nothing has worked yet. And I haven't been able to find any examples of how 'context' should be used.

关于如何使这种跨上下文请求过滤起作用的任何想法?可能吗?我想我可以通过将foo.war重命名为ROOT.war或更改根应用程序来实现我想做的事情,如所述

Any ideas on how I could get this kind of cross-context request filtering to work? Is it even possible? I imagine I might be able to achieve what I'm trying to do by renaming foo.war to ROOT.war or changing the root application as mentioned here, but I'd like to try doing this via URLRewrite unless doing so is infeasible or a bad idea on its face.

如果显示更多我的配置会有所帮助,请告诉我.预先感谢您的任何投入.

If showing more of my configuration would help, please let me know. Thanks in advance for any input.

修改:

感谢克里斯托弗·舒尔茨(Christopher Schultz)的有用答案.就我而言,问题是由两件事引起的:1)webapps/ROOT/META-INF中没有context.xml文件,以及2)webapps/中URL重写规则中的"to"元素中有错误ROOT/WEB-INF/urlrewrite.xml.

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

此修复程序涉及在webapps/ROOT/META-INF中放入适当的context.xml文件.为了给遇到此问题的其他人提供参考,该文件的最终外观如下:

The fix involved putting a proper context.xml file in webapps/ROOT/META-INF. For reference for anyone else that encounters this problem, that file ended up looking like this:

webapps/ROOT/META-INF/context.xml

webapps/ROOT/META-INF/context.xml

<?xml version='1.0' encoding='utf-8'?>

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

正如Schultz所提到的,仅需为给定URL重写规则(此处为ROOT)中'from'元素所隐含的上下文定义一个带有crossContext ="true"的上下文.不必在收件人" URL重写规则中为应用程序明确定义上下文.换句话说,您不需要为该应用程序手动创建一个context.xml文件-因此,继续上面的示例,您将不需要手动定义一个context.xml文件并将其放入webapps/foo/META-INF/.

As Schultz mentions, it's only necessary for a context with crossContext="true" to be defined for the context implied in the 'from' element in a given URL rewrite rule (here, that's ROOT). It is not necessary to explicitly define a context for the application in the 'to' URL rewrite rule. In other words, you shouldn't need to manually create a context.xml file for that application -- so continuing the example above, you would not need to manually define and put a context.xml file into webapps/foo/META-INF/.

舒尔茨的答案反映了官方Tomcat文档中定义上下文的建议:

Schultz's answer reflects the recommendations for defining a context in the official Tomcat documentation: http://tomcat.apache.org/tomcat-7.0-doc/config/context.html#Defining_a_context.

该问题还由以下事实引起:我的初始帖子中的URL重写规则存在错误.正确的版本应该是:

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

urlrewrite.xml:

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>

推荐答案

如果您的(实际)web应用程序已部署到/foo,并且您想即时重写URL,例如/group-elements/baz,以进行转发( 不是 重定向)到/foo/app/group/300245/elements,那么您将不得不将重写过滤器部署到以下两个位置之一:/group-elements/.

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

以上配置似乎正在部署到ROOT( /),然后将URL /baz映射到/foo/app/group/300245/elements.相反,您可能想要这样:

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

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

看来您正在尝试击打http://example.com/baz,我本来希望能奏效.魔术的最后一点是使ROOT上下文成为跨上下文(请注意,您的Web应用程序不必是跨上下文的:只有urlrewrite才可以).您可以通过将addc crossContext="true"更改为webapps/ROOT/META-INF/context.xml来将ROOT Web应用更改为跨上下文.

It looks like you were trying to hit http://example.com/baz which I would have expected to work. The last bit of magic is going to be making the ROOT context cross-context (note that your webapp does NOT need to be cross-context: only the urlrewrite one does). You can change the ROOT webapp to be cross-context by addint crossContext="true" to webapps/ROOT/META-INF/context.xml.

最后,您应该真正停止在server.xml中放置<Context>元素:基本上将它们保留在其中意味着您需要重新启动Tomcat才能更改Webapp部署.

Finally, you should really stop putting <Context> elements in server.xml: leaving them in there basically means you need to restart Tomcat in order to change your webapp deployments.

这篇关于在Tomcat中的上下文之间转发请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆