首页 文章

使用AJP与Tomcat 302重定向的Apache反向代理

提问于
浏览
1

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

我有一个Spring MVC 4网络应用程序,当我绕过代理访问它时效果非常好,但是,每当我通过代理并访问导致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 /

我错过了什么?

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
    

相关问题