我有一個獨立的tomcat服務器(前面沒有apache/nginx),我需要配置重新路由所有傳入請求及其標題爲特定應用程序不同主辦。換句話說,我需要的URL轉發請求和標題到不同的主機使用tomcat
https://host.company.com/app/<anything here after>
重定向到
https://newhost.company.com/app/<anything here after>
這樣
https://host.company.com/app/v1/explore?id=foo&pw=bar&more=crap
被重定向到
https://newhost.company.com/app/v1/explore?id=foo&pw=bar&more=crap
雖然不干擾https://host.company.com/app2
或https://host.company.com/app3
。
目前,我已經安裝在webapps/app
urlrewrite像這樣:
的webapps /應用/ WEB-INF/urlrewrite.xml:
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 4.0//EN" "https://www.tuckey.org/res/dtds/urlrewrite4.0.dtd"> <urlrewrite> <rule> <name>Requests to /app/ will be redirected to https://newhost.company.com/app/</name> <from>^/(.*)$</from> <to type="permanent-redirect" last="true">https://newhost.company.com/app/$1</to> </rule> </urlrewrite>
的webapps /應用/ WEB-INF/web.xml:
<filter> <filter-name>UrlRewriteFilter</filter-name> <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class> </filter> <filter-mapping> <filter-name>UrlRewriteFilter</filter-name> <url-pattern>/*</url-pattern> <dispatcher>REQUEST</dispatcher> <dispatcher>FORWARD</dispatcher> </filter-mapping>
重定向到https://newhost.company.com
正在工作,但是標題不會被轉發,這是使這項工作的關鍵因素。因此,雖然這工作得很好:
curl --compressed -H 'Accept-Encoding:gzip' --get 'https://newhost.company.com/app/v1/explore' --data-urlencode 'id=foo' --data-urlencode 'pw=bar' --data-urlencode 'more=crap'
這並不
curl --compressed -H 'Accept-Encoding:gzip' --get 'https://host.company.com/app/v1/explore' --data-urlencode 'id=foo' --data-urlencode 'pw=bar' --data-urlencode 'more=crap'
和運行以上-vv
揭示了問題(剝離下來的清晰度):
* Connected to host.company.com (w.x.y.z) port 443 (#0)
> GET /app/v1/explore?id=foo&pw=bar&more=crap HTTP/1.1
< HTTP/1.1 301 Moved Permanently
< Location: https://newhost.company.com/app/v1/explore
* Connected to newhost.company.com (w.x.y.z) port 443 (#1)
> GET /app/v1/explore HTTP/1.1
如您所見,GET to newhost.company.com不包含以'?'開頭的任何uri數據。在使用標準瀏覽器時,由此產生的URL是https://newhost.company.com/app/v1/explore
,這是合理的。但是,我需要將uri數據傳遞到newhost.company.com,並且我不知道如何讓tomcat執行此操作。
注:把阿帕奇/ nginx的/等在tomcat的面前是不是「安全」的宗旨