首页 > 解决方案 > haproxy 将 URL 路径重定向到另一个路径

问题描述

haproxy 版本 1.5.18

我想重定向:

/document/(.*)/(.*)/somefile => /anotherPath/somefile

例如:重定向

 /document/20181/20182/a_good_pic.jpg => /anotherPath/a_good_pic.jpg

如何用 haproxy 做到这一点?

我重试了 reqirep 的示例,例如:

# replace "/static/" with "/" at the beginning of any request path.
reqrep ^([^\ :]*)\ /static/(.*)     \1\ /\2

但是我的示例在 URL 路径中有两个部分与示例不同,所以我很困惑。

谢谢!

标签: pathurl-redirectionhaproxy

解决方案


您可以声明一个 acl,然后执行一个有条件的 use_backend 语句。像这样:

frontend a-frontend-conf

    # Declare an ACL using path_beg (Path Begins)
    acl path_images path_beg /images

    # Use backend server1 if acl condition path_images is fulfilled
    use_backend server1 if path_images

backend server1
    [...]

来源:https ://serverfault.com/questions/659793/haproxy-how-to-balance-traffic-within-directory-reached


推荐阅读