Haproxy 1.7 url rewriting reqrep doesn’t work

haproxyrewrite

I'm currently trying to rewrite url based on extensions and subdomains, but my update doesn't work, so I will try to explain my procedure:

I would like to rewrite this kind of url

https://mydomain.tld/image.jpg

to

https://jpg.mydomain.tld/image

so I update my haproxy.cfg and add to my frontend these rules:

frontend web
    bind *:80
    acl p_ext_jpg path_end -i .jpg
    acl p_ext_png path_end -i .png
    acl mydomain hdr(host) -i mydomain.tld
    reqrep ^([^\ :]*)\ /(*).(jpg|png)     \1\ /\2
    use_backend backend_static if  p_ext_jpg p_ext_png mydomain
    default_backend backend_web

My current nginx rule who work:

rewrite "^/([0-9]+).(jpg|png)$" $scheme://$2.mydomain.tld/$1;

My current problems:

  1. I don't know how to replace mydomain by (jpg|png).mydomain
  2. any reqrep seems not be used by my haproxy

I only want to rewrite url and forward to my backend not redirection.

Thanks for your help and sorry for my english.

Best Answer

Your backend is never used because your "if" is an implicit AND. Use an "or":

use_backend backend_static if mydomain { p_ext_jpg or p_ext_png }

And so you can rewrite the host:

reqirep ^Host:\ mydomain.tld Host:\ jpg.mydomain.tld if mydomain p_ext_jpg
reqirep ^Host:\ mydomain.tld Host:\ png.mydomain.tld if mydomain p_ext_png

"http-request" is a "newer" way to solve this...