-1

我一直在修补 Apache + Tomcat,这样我就可以通过 apache(干净清晰的 urls rock)为多个 tomcat 应用程序(在不同的机器上)提供服务。我已经成功配置mod_proxy_ajp&到可以在不同机器上提供两个 tomcat 应用程序几乎没有问题mod_rewrite的地步。

我发现的唯一问题是其中一个应用程序(我在 Struts2 中开发)有很多链接和表单,它们是用<s:a />,<s:url /><s:form />标签生成的。这些标签生成的url一般是这样的:

/WebApp/path/to/some.action

多亏了 ModRewrite 的魔力,这通常不是一个问题,指向此类 url 的超链接会很快被重写并重定向到/app/path/to/some.action(尽管我确实得到了大量的302响应)。

真正的问题发生在执行 POST 请求时。大家可能都知道,我无法POST使用 mod_rewrite 重定向请求......所以最后......我的所有POST请求都不起作用,因为 mod_rewrite 重定向到正确的 url,而是作为GET请求。

我已经阅读了一些关于mod_proxy_html它如何帮助我重写 Tomcat Web 应用程序返回的 url……但感觉很麻烦。

这是我当前的 apache 配置:

## HACKING BEGINS RIGHT HERE

# cookies
ProxyPassReverseCookiePath /WebApp /app
# this is for CSS, IMGs, JS and redirecting urls with /WebApp*
RewriteRule ^/WebApp(.*)$ /app$1 [R,L]
<Location /app>
    ProxyPass ajp://localhost:8009/WebApp
    ProxyPassReverse ajp://localhost:8009/WebApp
    Order allow,deny
    Allow from all
</Location>

# the other app
ProxyPassReverseCookiePath /WebApp2 /other
<Location /other>
    ProxyPass ajp://200.9.4.6:8009/WebApp2
    ProxyPassReverse ajp://200.9.4.6:8009/WebApp2
    Order allow,deny
    Allow from all
</Location>

我的请求问题必须有解决方案POST......有什么想法吗?我能以某种方式配置一些允许 Struts2 输出正确 url 的东西吗?

谢谢你的帮助。

4

3 回答 3

0

This is a problem with the webapp setup; there may be a way to work around it in httpd config, but it's cleaner to fix the original inconsistency vs. hacking around it.

Also worth mentioning that Struts is trying to help you by giving you context-aware, non-relative URLs; if you just make the paths match, you can rely on that (instead of fighting it...).

So, just deploy the Struts webapp to /app and /other, instead of /WebApp and /WebApp2. (There might be some reason this isn't possible -- like if you don't actually control those other servers -- but it sounds like that doesn't apply).

于 2012-06-29T20:46:34.930 回答
0

可能有几种方法可以解决这个问题。

如果您能够以您在 URL 中使用的相同名称将您的应用程序部署到 Tomcat,那将是最简单的。因此,您的应用程序app/webapps/app[.war]代替/webapps/WebApp,这将完全避免重写。

否则,以下应该有效,并且应该在您当前的重写规则之前进行。[PT] 应该允许 Apache 仍然代理请求(这在使用时有效,mod_jk不确定我是否使用/测试过它mod_proxy_ajp):

RewriteCond %{REQUEST_METHOD} POST
RewriteRule ^/WebApp(.*)$ /app$1 [PT,L]

一旦您的 bean 处理了 POST 请求,您就可以将请求发送到将被重定向的 URL。最后,POST URL 对用户来说并不重要,所以它不必是一个漂亮的 URL。

以下编辑:

我在 Apache mod_proxy 上看到 ProxyPassReverse 应该与该[PT]标志一起使用。而且我能够复制您遇到的问题,并且下面的配置适用于我的基本 JSP 页面,其表单发布到另一个 JSP 页面。

<VirtualHost *:80>
    ServerName localhost
    RewriteEngine On

    RewriteCond %{REQUEST_METHOD} !POST
    RewriteRule /WebApp/(.*) /app/$1 [R,L] 

    RewriteCond %{REQUEST_METHOD} POST
    RewriteRule /WebApp/(.*) /app/$1 [PT,L]

    <Location /app>
        ProxyPass ajp://localhost:8109/WebApp
        ProxyPassReverse ajp://localhost:8109/WebApp
    </Location>
</VirtualHost>
于 2011-03-30T14:29:24.747 回答
-1

我会避免 URL 重写。它只是打开像这样的蠕虫罐,让您查看 mod_proxy_html 等,当然这些只是更多的蠕虫。相反,只需将您的应用程序部署在 Tomcat 中,并使用您可以在外部可见的真实 URL。

于 2012-09-30T22:41:15.443 回答