HAProxy reqrep path backend frontend

haproxy

I would like to add a path in my backend in haproxy.
I don't want to use redirect. So I try to use reqrep

Basically what I need:

frontend:

resources.mydomain.com/images/path/to/resource.png

Then it needs to forward to:

backend.mydomain.com/replaced/part/path/to/resource.png

Here is my cfg

frontend http-in
    bind 0.0.0.0:80
    mode http
    option httplog
    acl path_ok path_end .gif
    acl path_ok path_end .jpg
    acl path_ok path_end .png
    http-request deny unless path_ok
    use_backend resourceBackend if path_ok

backend resourceBackend
    reqrep ^([^\ :]*)\ /images[/]?(.*) \1\/replaced/part/\2
    mode http
    option httpchk
    option forwardfor except 127.0.0.1
    http-request add-header X-Forwarded-Proto https if { ssl_fc }
    server web-server1 backend.mydomain.com  maxconn 32

I tested my regexp on https://regex101.com/ and it seems to work.

Best Answer

In fact I forgot a space in the replacement:

frontend http-in
    bind 0.0.0.0:80
    mode http
    option httplog
    acl path_ok path_end .gif
    acl path_ok path_end .jpg
    acl path_ok path_end .png
    http-request deny unless path_ok
    use_backend resourceBackend if path_ok

backend resourceBackend
     # Space before /replaced
     reqrep ^([^\ :]*)\ /images[/]?(.*) \1\ /replaced/part/\2
     mode http
     option httpchk
     option forwardfor except 127.0.0.1
     http-request add-header X-Forwarded-Proto https if { ssl_fc }
     server web-server1 backend.mydomain.com  maxconn 328