【问题标题】:Apache reverse proxy using AJP with Tomcat 302 redirects使用 AJP 和 Tomcat 302 重定向的 Apache 反向代理
【发布时间】:2017-07-24 10:52:43
【问题描述】:

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

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

直接我会被重定向到正确的路径,所以我知道不是 Web 应用程序向客户端提供了错误的 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 /

我错过了什么?

【问题讨论】:

    标签: apache reverse-proxy http-status-code-302 ajp tomcat8.5


    【解决方案1】:

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

    # 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-09-16
      • 2016-05-17
      • 2015-10-10
      • 1970-01-01
      • 2010-10-31
      • 1970-01-01
      • 1970-01-01
      • 2021-01-31
      相关资源
      最近更新 更多