我使用mod_proxy在Apache httpd背后有一个相当常见的tomcat应用服务器设置。 我的虚拟主机看起来像这样:
<VirtualHost test.example.be:80> ServerName test.example.be RewriteEngine on RewriteRule ^/test/(.*)$ /$1 ProxyRequests Off ProxyPreserveHost On <Proxy *> Order deny,allow Allow from all </Proxy> ProxyPass / http://localhost:8181/test/ ProxyPassReverse / http://localhost:8181/test/ </VirtualHost>
在tomcat中部署的应用程序'test'使用一个上下文感知的框架(spring安全性),因此在redirect时预先加载上下文根。 所以,当它redirect到一个名为foo.html的页面时,它会redirect到bleh://test.example.com/test/foo.html。 这个URL不存在,并给出了404。由于我无法控制上下文根的前置,我想我可以解决这个使用mod_rewrite,通过删除上下文根。 不幸的是,这不起作用:就好像RewriteRule完全被忽略。
我注意到一件奇怪的事情:对bleh://test.example.com/test/foo.html的请求甚至不会被转发给tomcat:我从Apache httpd中得到熟悉的404页面。 如果我input任何其他伪造的url,如bleh://test.example.com/jfaklkfdjljfkl,我从tomcat获得404页面。 我希望bleh://test.example.com/test/foo.html可以调用bleh:// localhost:8181 / test / test / foo.html,但显然我在这里丢失了一些东西。
我远离这些事情的专家,所以任何帮助将不胜感激。
UPDATE
我在这个问题上取得了一些进展。 我被告知可以从mod_rewriteloggingdebugging输出,所以我做了这个。 以下是这些日志的相关部分:
[12/Oct/2011:10:57:09 +0200] [test.example.be/sid#7b1bc8][rid#820520/initial] (2) init rewrite engine with requested uri /test/notify [12/Oct/2011:10:57:09 +0200] [test.example.be/sid#7b1bc8][rid#820520/initial] (3) applying pattern '^/test/(.*)$' to uri '/test/notify' [12/Oct/2011:10:57:09 +0200] [test.example.be/sid#7b1bc8][rid#820520/initial] (2) rewrite '/test/notify' -> '/notify' [12/Oct/2011:10:57:09 +0200] [test.example.be/sid#7b1bc8][rid#820520/initial] (2) local path result: /notify [12/Oct/2011:10:57:09 +0200] [test.example.be/sid#7b1bc8][rid#820520/initial] (2) prefixed with document_root to /usr/local/apache/htdocs/notify [12/Oct/2011:10:57:09 +0200] [test.example.be/sid#7b1bc8][rid#820520/initial] (1) go-ahead with /usr/local/apache/htdocs/notify [OK]
所以这里发生的事情是,mod_rewrite正在做它应该做的,但由于某种原因,Apache不会通过URL到mod_proxy,而是它prepends / usr / local / apache / htdocs并试图解决这个问题。 所以新的问题是:我如何让Apache不prepend / usr / local / apache / htdocs,只是通过URL传递给tomcat?
PS:我不得不在几个地方用'bleh'来代替'http',因为显然你可以发布的链接数量是有限的。
只需使用[P]标志将请求传递给mod_proxy:
RewriteRule ^/test/(.*)$ http://localhost:8181/$1 [P]
没有前置DocumentRoot的正确方法是使用RewriteBase指令,或将PT(PassThrough)标志添加到重写规则中。 看到这里 – httpd.apache.org/docs/current/rewrite/flags.html#flag_pt