Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

regex - Haproxy route and rewrite based on URI path

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

https://www.example.com/v1/catalog/foo/bar

Should lead to the "catalog-v1" backends.

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

TO

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

Tried the following regex which worked on regex101:

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

Substitution:

1/3-2/4

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

Am I missing something?

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.

Thanks in advance for your help!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If I understand you correctly, replace the example below:

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

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

reqrep search string [{if | unless} cond]

You have no spaces.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...