阿帕奇使用反向代理AJP在Tomcat 302重定向
问题描述:
我完全摆脱了与此一想法,并认为我需要争取别人的帮助与Apache HTTPD有很好的理解(2.4)和Tomcat(8.5)阿帕奇使用反向代理AJP在Tomcat 302重定向
我有一个Spring MVC 4 web应用程序,当我绕过代理访问它时,它工作的非常好,但是,每当我通过代理访问并访问导致302重定向的链接时,它都会失败。
直接转到我得到重定向到正确的路径,所以我知道它不是提供错误的URL回到客户端的Web应用程序。但是通过代理我会被重定向到一个位置,这个位置似乎在URL前加上了上下文路径 - 这已经存在了!所以它出现两次,因此有一个不存在的URL的请求!
当我看着Tomcat s访问日志我可以用它的上下文路径前缀302重定向的路径 - 一个重复的上下文路径!
80.229.100.100 - - [04/Mar/2017:08:07:54 +0000] "GET /ctxPath/redirect HTTP/1.1" 302 -
80.229.100.100 - - [04/Mar/2017:08:07:54 +0000] "GET /ctxPath/ctxPath/testUrl HTTP/1.1" 404 986
这必须是Tomcat对使用AJP连接器的HTTPD的响应。当直接访问页面时,它通过HTTP连接器并正常工作。我更新了HTTPD配置以使用HTTP连接器,并获得302重定向的相同结果。
因此,您可以看到,每个重定向都会导致404错误。我是否必须以某种方式更改Tomcat的配置?
目前我的httpd的配置看起来像这样(端口9009是正确的,因为我有多个Tomcat安装):
ProxyPass/ajp://localhost:9009/ctxPath/
ProxyPassReverse/ajp://localhost:9009/ctxPath/
ProxyPassReverseCookiePath /ctxPath/
我缺少什么?
答
我知道现在这已经很老了,但是我前段时间确实解决了这个问题,所以认为值得发布我的修复程序 - 不确定它是否是'正确'的方式,但它似乎已经完成了去年!
# HTTP 302 redirects are not modified by the reverse proxy when using the AJP protocol.
# https://tomcat.apache.org/connectors-doc/common_howto/proxy.html
# Or perhaps Tomcat needs further configuration to support a proxy (See Connector config)
# When sending a redirect, the redirect URL is placed in the 'Location' HTTP response header.
# When the browser requests the page using the path in the Location header,
# the proxy will dutifully proxy the request to the backend - which prefixes the context path.
# This will cause the context path to appear twice and result in a 404.
# ProxyPassReverse would usually modify the Location HTTP header, but using AJP it
# appears no to, so take care of this ourselves by removing the context path from the
# URL in the Location header before passing it back to the client.
# Spring Security uses redirects during authentication
Header edit Location /ctxPath/(.*)$ /$1