0

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.

Geoffrey
  • 125
  • 1
  • 6

2 Answers2

1

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
gxx
  • 5,591
  • 2
  • 22
  • 42
Geoffrey
  • 125
  • 1
  • 6
  • Could you move your comment into a separate line and put a `#` in front? Doing it this way would allow the code to work... – gxx Oct 09 '17 at 06:02
0

To better understand what reqrep does and how it works you can checkout this https://github.com/kamleshchandnani/haproxy/blob/master/haproxy-reqrep.md

  • While providing links is useful, it would be MORE helpful to the asker if you posted a summary of the steps involved on the link you posted. For more tips, see [How to answer](https://serverfault.com/questions/how-to-answer). – Daniele Santi Jan 29 '19 at 08:35