Haproxy路由和基于URI路径的重写

编程入门 行业动态 更新时间:2024-10-27 08:25:11
本文介绍了Haproxy路由和基于URI路径的重写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试设置Haproxy以在uri路径标识的一些后端上负载均衡请求.例如:

I am trying to setup an Haproxy to load balance requests on a few backends identified by the uri path. For example:

www.example/v1/catalog/foo/bar

应该导致"catalog-v1"后端.

Should lead to the "catalog-v1" backends.

每个应用程序的响应路径都不相同,因此,我不仅必须识别该应用程序,还必须重写URL路径.例如

Thing is each app responds on a different path so I must not only identify the app but rewrite the URL path. E.g.

  • www.example/v1/catalog/product
  • www.example/v2-2/checkout/购物车/123
  • www.example/v3.1.2/checkout/cart
  • www.example/v1/catalog/product
  • www.example/v2-2/checkout/cart/123
  • www.example/v3.1.2/checkout/cart

TO

  • www.example/catalog-v1/product
  • www.example/checkout-v2-2/购物车/123
  • www.example/checkout-v3.1.2/cart
  • www.example/catalog-v1/product
  • www.example/checkout-v2-2/cart/123
  • www.example/checkout-v3.1.2/cart

我知道我不应该将Haproxy用于重写目的,但是现在这是不可避免的.

I know I shouldn't use Haproxy for rewriting purposes but for now this is inevitable.

尝试了以下可用于regex101的正则表达式:

Tried the following regex which worked on regex101:

([a-z.]*)\/([a-z0-9\-\.]*)\/([a-z\-]*)\/(.*)

替换:

\1/\3-\2/\4

最后是haproxy.config:

And finally here's the haproxy.config:

global daemon user root group root maxconn 256000 log 127.0.0.1 local0 log 127.0.0.1 local1 notice stats socket /run/haproxy/stats.sock mode 777 level admin defaults log global option dontlognull maxconn 4000 retries 3 timeout connect 5s timeout client 1m timeout server 1m option redispatch balance roundrobin listen stats :8088 mode http stats enable stats uri /haproxy stats refresh 5s backend catalog-v1 mode http option httpchk GET /catalog-v1/ping http-check expect status 200 reqrep ([a-z.]*)\/([a-z0-9\-\.]*)\/([a-z\-]*)\/(.*) \1/\3-\2/\4 server 127.0.0.1:8280_catalog-v1-node01 127.0.0.1:8280 check inter 2s rise 3 fall 2 backend checkout-v1 mode http option httpchk GET /checkout-v1/ping http-check expect status 200 reqrep ([a-z.]*)\/([a-z0-9\-\.]*)\/([a-z\-]*)\/(.*) \1/\3-\2/\4 server 127.0.0.1:8180_checkout-v1-node01 127.0.0.1:8180 check inter 2s rise 3 fall 2 frontend shared-frontend mode http bind localhost:80 acl is-catalog-v1-path path_dir /v1/catalog acl is-checkout-v1-path path_dir /v1/checkout use_backend catalog-v1 if is-catalog-v1-path use_backend checkout-v1 if is-checkout-v1-path

我想念什么吗?

我已经为此苦苦挣扎了很长时间,但都没有成功.后端在Haproxy的统计信息页面中显示"UP",但是每次我调用未重写的url"时,都会收到400错误的请求错误.

I've been struggling with this for quite some time without success. The backends shows "UP" in Haproxy stats page but everytime I call the "non rewrited url" I get a 400 Bad Request error.

提前感谢您的帮助!

推荐答案

如果我对您的理解正确,请替换以下示例:

If I understand you correctly, replace the example below:

reqrep ^([^\ ]\*)\ /([-.0-9A-Za-z]\*)/([a-zA-Z]\*)/(.\*) \1\ /\3-\2/\4

cbonte.github.io/haproxy-dconv/configuration-1.5.html#reqrep

reqrep search string [{if | unless} cond]

您没有空格.

更多推荐

Haproxy路由和基于URI路径的重写

本文发布于:2023-10-26 01:09:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1528633.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重写   路由   路径   Haproxy   URI

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!