0

Used HAProxy version: v2.6.6

What I’m trying to achieve is that when a user visit http://haproxyserver.domain.com/application1, HAProxy gets the page at https://appserver1.otherdomain.com/ and send it back as http://haproxyserver.domain.com/application1

So, I want to rewrite “/application1” from the front to “/” in the back and send the response back as “/application1”. E.g. an image on the backend like https://appserver1.otherdomain.com/images/test.jpg should be send back as http://haproxyserver.domain.com/application1/images/test.jpg and so on.

My haproxy.cfg file without any rewrite config in it:

#---------------------------------------------------------------------
# PRD http frontend which proxys to the backends
#---------------------------------------------------------------------
frontend  PRD-http-front
bind \*:80
http-request add-header X-Forwarded-For %\[src\]
http-request add-header X-Forwarded-Proto http

    mode http
    option httplog
    option forwardfor       except 127.0.0.0/8
    maxconn 10240
    
    # define acl's
    acl host_haproxy-stats hdr(host) -i haproxy-stats.domain.com
    
    # decide which backend to use
    # HAProxy statistics page
    use_backend haproxy-stats if host_haproxy-stats
    
    # Other
    use_backend application1 if { path_beg /application1 }
    
    # default backend
    default_backend PRD-local-httpd

#---------------------------------------------------------------------
# haproxy-stats backend
#---------------------------------------------------------------------
backend haproxy-stats
mode http
option httpclose
option forwardfor
http-request add-header X-Forwarded-For %\[src\]
http-request add-header X-Forwarded-Proto http
server haproxy-stats 127.0.0.1:8888 check

#---------------------------------------------------------------------
# PRD-local-httpd backend
#---------------------------------------------------------------------
backend PRD-local-httpd
mode http
option httpclose
option forwardfor
http-request add-header X-Forwarded-For %\[src\]
http-request add-header X-Forwarded-Proto http
server HAProxy-Server haproxyserver.domain.com:8081 check

#---------------------------------------------------------------------
# application1 backend
#---------------------------------------------------------------------
backend application1
mode http
option httpclose
option forwardfor
http-request add-header X-Forwarded-For %\[src\]
http-request add-header X-Forwarded-Proto http
server appserver1 appserver1.otherdomain.com:443 check ssl verify none

This config doesn’t work because HAProxy will try to load https://appserver1.otherdomain.com/application1 in the backend, which doesn’t exist. I Already tried several rewrite settings in the backend config part without any success. Some examples:

http-request replace-path ^([^\ ])\ /application1/(.) \1\ /\2
http-request replace-path /application1(.*) / \1

I Even tried this in the frontend:

acl app1 path_beg -i /application1/
http-request set-path  /%[path] if app1

I tried many things, but I can’t figure it out. Every help is much appreciated!

Opper
  • 1
  • 1
  • 3

1 Answers1

0

In your backend application1 add host rewriting: http-request set-header host appserver1.otherdomain.com and path rewriting as you did in your question. I'm not sure if you need to rewrite anything in response, but if you do, then it will same as in request, just use http-response instead.

tbielaszewski
  • 739
  • 3
  • 6
  • Thank you very much for taking the time to reply! I've now added the `http-request set-header host appserver1.otherdomain.com` and `http-request replace-path ^([^\ ])\ /application1/(.) \1\ /\2` This got me a little bit closer. Unfortunately, `replace-path` doesn't exist in `http-response`. – Opper Dec 08 '22 at 14:24
  • Apparently there is no path in HTTP response and nothing sends host header in the response, so no need to do these. – tbielaszewski Dec 08 '22 at 15:11
  • Well, I'm afraid it is not that simpel. The login page now loads but instead of showing `http://haproxyserver.domain.com/application1/login` it shows `http://haproxyserver.domain.com/login`. Clicking the login button sends it to the root (/) and it is no longer proxied to the correct backend. – Opper Dec 08 '22 at 15:40
  • Links in HTML body generated by application are best set properly by application. HAProxy doesn't modify body of requests and responses. – tbielaszewski Dec 08 '22 at 16:05
  • I Understand that, but the links are relative in the application. So the response is `/login` and I would like that to be rewritten to `/application1/login` ... – Opper Dec 08 '22 at 16:19