如何在haproxy中添加uri前缀



我有一个像这样的haproxy配置:

frontend api
    mode http
    default_backend tomcat
backend tomcat
    mode http
    balance roundrobin
    option httpchk HEAD / HTTP/1.0
    server tomcat1 10.0.0.1:1234 weight 1 maxconn 512 check
    server tomcat2 10.0.0.2:1234 weight 1 maxconn 512 check

这是可行的,但URL必须如下:http://api.example.com/project/api/get-something我们想以某种方式设置haproxy,以便urlhttp://api.example.com/api/get-something将得到相同的结果。

我尝试添加url前缀:

    server tomcat1 10.0.0.1:1234/project/ weight 1 maxconn 512 check
    server tomcat2 10.0.0.2:1234/project/ weight 1 maxconn 512 check

但它不受支持。有没有办法我只用haproxy就能做到这一点?我不想仅仅为了代理tomcat而设置apache。

根据我的理解,您想要将/api/重写为/project/

如果是,则在后端配置中添加以下行:

reqrep ^([^ :]*) /api/(.*)     1 /project/2

请注意,您必须在前端和服务器上启用选项http server close,才能将重写规则应用于会话中的所有请求。

最新更新