1

我对这个完全没有想法,并认为我需要寻求对 Apache HTTPD (2.4) 和 Tomcat (8.5) 有很好理解的人的帮助

我有一个 Spring MVC 4 Web 应用程序,当我绕过代理访问它时,它运行良好,但是,每当我通过代理访问导致 302 重定向的链接时,它都会失败。

直接去我会被重定向到正确的路径,所以我知道不是网络应用程序向客户端提供了错误的 URL。但是通过代理,我被重定向到一个位置,该位置似乎以上下文路径作为 URL 的前缀——它已经存在!所以它出现了两次,因此请求的 URL 不存在!

当我查看Tomcat的访问日志时,我可以在 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 对 HTTPD 的响应——它使用 AJP 连接器。当直接访问该页面时,它通过 HTTP 连接器并且工作正常。我更新了我的 HTTPD 配置以使用 HTTP 连接器并为 302 重定向获得相同的结果。

如您所见,每次重定向都会导致 404。我是否必须以某种方式更改 Tomcat 的配置?

目前我的 HTTPD 配置看起来像这样(端口 9009 是正确的,因为我有多个 Tomcat 安装):

ProxyPass / ajp://localhost:9009/ctxPath/
ProxyPassReverse / ajp://localhost:9009/ctxPath/
ProxyPassReverseCookiePath /ctxPath /

我错过了什么?

4

1 回答 1

0

我知道这现在已经很老了,但我确实在前一段时间解决了它,所以认为值得发布我的修复 - 不确定这是否是“正确”的方式,但它似乎在去年完成了这项工作!

# 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
于 2018-03-07T09:16:04.370 回答