简体   繁体   中英

How can I rewrite URLs in HAProxy?

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”. Eg 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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM