apache - 如何在使用 Apache 的代理设置中重写位置响应 header ?

标签 apache url-rewriting reverse-proxy response-headers

我有一个主代理,它将请求发送到安装了 OpeenSSO 的辅助代理。

如果 OpenSSO 代理确定用户未登录,则会向身份验证服务器发出 302 重定向,并提供用户请求的原始(编码)URL 作为重定向位置 header 中的 GET 参数。

但是,GET 变量中的 URL 是内部(辅助)代理服务器的 URL,而不是原始代理服务器的 URL。因此,我想编辑/重写“位置”响应 header 以提供正确的 URL。

例如。

  • http://a.com/hello/ (原始请求 URL)
  • http://a.com/hello2/ (带有 OpenSSO 代理的辅助代理)
  • http://auth.a.com/login/?orig_request=http%3A%2F%2Fa.com%2Fhello2%2F (302 重定向到身份验证服务器,请求的第二个代理服务器的 URL 编码在 GET 变量中)
  • http://auth.a.com/login/?orig_request=http%3A%2F%2Fa.com%2Fhello%2F (编码后的 URL 被重写为原始请求的 URL)

  • 我已经尝试了几乎所有标题和重写的组合而没有运气,所以我认为这可能是不可能的。我得到的最接近的是这个,但是 mod_headers 编辑函数不解析环境变量。
    # On the primary proxy.
    RewriteEngine On
    RewriteRule ^/(.*)$ - [E=orig_request:$1,P]
    Header edit Location ^(http://auth\.a\.com/login/\?orig_request=).*$ "$1http%3A%2F%2Fa.com%2F%{orig_request}e"
    

    最佳答案

    代理密码反向

    ProxyPassReverse应该为你做这个:

    This directive lets Apache adjust the URL in the Location, Content-Location and URI headers on HTTP redirect responses.



    假设您使用一对 P​​roxyPass 和 ProxyPassReverse 指令来定义它,我不确定为什么您的反向代理还没有这种行为。

    编辑位置标题

    如果您希望能够按照您的描述编辑 Location 标题,您可以这样做 as of Apache 2.4.7 :

    For edit there is both a value argument which is a regular expression, and an additional replacement string. As of version 2.4.7 the replacement string may also contain format specifiers.



    文档中提到的“格式说明符”包括能够使用环境变量,例如%{VAR}e .

    您可能还想考虑修改您的应用程序,使 orig_request URL 参数相对化,从而可能不需要使用环境变量进行 Header 编辑。

    相对路径位置 header

    您还可以尝试在 Location header 中使用相对路径,这样就无需将一个域显式映射到另一个域。这是正式有效的as of RFC 7231 (2014 年 6 月),but was was widely supported even before that .您可以使用 Apache Header edit 相对化您的 Location header 指令(甚至在 2.4.7 版之前,因为它不需要环境变量替换)。这看起来像这样:
    Header edit Location "(^http[s]?://)([a-zA-Z0-9\.\-]+)(:\d+)?/" "/"
    

    关于apache - 如何在使用 Apache 的代理设置中重写位置响应 header ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16297233/

    相关文章:

    php - htaccess 文件适用于 apache 2.2,但不适用于 apache 2.4 - aws beanstalk 托管

    php - 重建 VM 后 apache2 中的 Symfony2 URL 404

    jquery - 哈希后将数据存储在 URI 中

    asp.net - IIS URL 重写 : 403 - Forbidden: Access is denied

    html - 使用 apache 注入(inject) html

    java - 我已经设置了 apache tomcat 8.5

    asp.net - 通用 Web.Config 重写以删除查询字符串并添加 url seo slug

    svn - 为什么 VisualSVN 服务器会重写我的 URL?

    caching - 什么时候应该在vcl_recv和vcl_fetch下添加相同的策略?

    angular - 如何在 Angular 2 中设置反向代理