2017-03-04 306 views
1

我完全擺脫了與此一想法,並認爲我需要爭取別人的幫助與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/

我缺少什麼?

回答

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