使用tomcat将请求和标头转发到不同的主机

时间:2014-09-10 14:40:42

标签: tomcat redirect header http-headers request

我有一个独立的tomcat服务器(前面没有apache / nginx),我需要将其配置为将特定应用程序的所有传入请求及其标题重新路由到其他主机。换句话说,我需要URL

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/app2https://host.company.com/app3

目前,webapps/app已安装urlrewrite,如此:

  1. web应用/应用/ 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>
    
  2. web应用/应用/ 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>
    
  3. 重定向到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
    

    正如您所看到的,对newhost.company.com的GET不包含以“?”开头的任何uri数据。使用标准浏览器时,生成的URL为https://newhost.company.com/app/v1/explore,鉴于上述情况,这是有意义的。但是,我需要将uri数据传递给newhost.company.com,而我不知道如何让tomcat执行此操作。

    注意:在tomcat之前放置apache / nginx / etc不是“安全”目的的选项(不是我的要求,来自管理层的命令)

1 个答案:

答案 0 :(得分:0)

使用urlrewrite时,您需要将use-query-string =“true”添加到urlrewrite.xml urlrewrite标记中,以便在转发的URL上传递查询字符串参数。

改变自:

<urlrewrite>

更改为:

<urlrewrite use-query-string="true">