使用重定向配置重写 HAProxy 和 reqrep 路径

HAProxy and reqrep path rewriting with redirect configuration

使用 HA Proxy 1.5 我需要重写 URL 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(.*)    /other-bar
  server myserver othersite.com:8081

这个有效: URL
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

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

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

HA Proxy 可以吗?我尝试了很多配置都没有成功。 谢谢!

如果您在 HTML 中谈论 link(相对于 Location: headers 重定向)...HAProxy 1.5 不会无法修改这些。

据推测,根据您的描述,页面 /other-bar/page1.html 在内部 link 到 <a href="/other-bar/page2.html"> 而它确实应该 link 到 <a href="page2.html">。你需要相对的 links 才能使这样的事情透明地工作......否则,链中的组件 "X" 将必须能够动态修改 links在响应 body 中,但只有 links,当然,因为你不想盲目地 regex-replace 整个页面内容......而且 HAProxy 1.5 不会改变响应体,所以它不能完成组件的角色 "X."

HAProxy 1.6 可能 可以用 Lua 做到这一点,但这是一个可能......如果它可以做到,那就是可能达到您通常期望从 HAProxy 获得的性能水平,因为擦除 Lua 中的 html 可能是一个相对昂贵的提议。

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