【问题标题】:HAProxy and reqrep path rewriting with redirect configuration使用重定向配置重写 HAProxy 和 reqrep 路径
【发布时间】:2018-06-30 22:41:41
【问题描述】:

使用 HA Proxy 1.5 我需要从 http://main.domain.com/my-foohttp://othersite.com:8081/other-bar

这是我尝试过的:

frontend ft_def
  bind :80                                      
  mode http                                                  
  acl has_special_uri path_beg /my-foo
  use_backend def if has_special_uri
  default_backend def

backend def
  mode http                                       
  option forwardfor                       
  reqirep ^([^\ ]*\ )/my-foo(.*)    \1/other-bar\2
  server myserver othersite.com:8081

这有效: 网址
http://main.domain.com/my-foo/home.html
变成了
http://othersite.com:8081/other-bar/home.html

并在浏览器中显示初始 URL http://main.domain.com/my-foo/home.html

这正是我所需要的:它对用户来说是完全透明的。 但是重定向不起作用:当我点击页面上的链接时,URL 是
http://main.domain.com/other-bar/page2.html

我想让http://main.domain.com/my-foo/page2.html 出现在浏览器中。

是否可以使用 HA 代理?我尝试了很多配置都没有成功。 谢谢!

【问题讨论】:

    标签: regex redirect url-rewriting haproxy httpbackend


    【解决方案1】:

    如果您谈论的是 HTML 中的链接(而不是 Location: 重定向标头)... HAProxy 1.5 将无法修改这些链接。

    据推测,根据您的描述,页面/other-bar/page1.html 在内部链接到<a href="/other-bar/page2.html">,而实际上它应该链接到<a href="page2.html">。您需要相对链接才能使这样的事情透明地工作......否则,您的链中的组件“X”将必须能够在响应正文中动态修改链接,但当然只有链接,因为您不想盲目地将页面内容作为一个整体进行正则表达式替换……而且 HAProxy 1.5 不会处理响应主体,因此它无法履行组件“X”的角色。

    HAProxy 1.6 可能可以用 Lua 做到这一点,但这是一个可能......如果可以做到,它不太可能达到您通常期望从 HAProxy 获得的性能,因为在 Lua 中清理 html 可能是一个相对昂贵的提议。

    【讨论】:

      【解决方案2】:

      通常情况下,任何 URL 更改都应由 Web 应用程序本身处理,例如在Phoenix.Endpoint:url 配置下有一个:path 选项,用于Phoenix 框架:https://hexdocs.pm/phoenix/Phoenix.Endpoint.html,专为此目的而设计。然后浏览器仍会请求该 URL 出现在 HTML 文件中,但 HAProxy 将始终重写它。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-10-02
        • 1970-01-01
        • 2012-12-10
        • 2016-09-20
        • 2012-01-02
        • 2014-02-12
        • 2015-10-27
        • 2014-09-07
        相关资源
        最近更新 更多