0

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.

Romain
  • 26
  • 3
  • I don't have a full answer, but you're going to need to use some combination of `http-request` directives because you're making changes to more than just a single header here (as your `reqrep` is doing). You'll probably need to use `http-request` with `set-uri`, `set-path`, `set-uri`, `set-header`, or `replace-header`. – GregL Feb 07 '17 at 12:56

1 Answers1

0

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...

  • is it also possible to remove the port number from the url? – Vini Nov 21 '18 at 19:53
  • the port number is in the host header - so yes... you can... [haproxy docu](https://cbonte.github.io/haproxy-dconv/1.7/configuration.html#4.2-reqrep) example url: http://example.com:12345/bla.html `reqirep ^Host:\ example.com:12345 Host:\ example.com` – Lutz Reinhardt Nov 22 '18 at 20:21
  • `reqirep` was not recognized on my haproxy 1.6 – Vini Nov 25 '18 at 18:17
  • http://cbonte.github.io/haproxy-dconv/1.6/configuration.html#4.2-reqirep – Lutz Reinhardt Nov 26 '18 at 19:18