I have a HTTPS server and want to redirect the specific request based on the URL to my Backend server

frontend http
bind *:80

frontend http
bind *:80
acl mpd path_end -i .mpd
acl test hdr(host) test.usp.com https://test.usp.com
use_backend internal if test mpd

backend internal
http-request set-path /vod/
server testirdeto.test.com 172.28.1.16:80 check

Hi,

You may redirect specific request to a specific backend server based on the URL using ACLs. To achieve this, you might want to use the path fetch methods in the test criterion of the ACL such that the ACL returns true if the criterion is satisfied.

The path method extracts the request’s URL path, which starts at the first slash and ends before the question mark (without the host part) and matches it with the value provided in the ACL. Depending on your requirements, you may also make use of other path method derivatives such as path_beg, path_end and path_sub to extract and match the prefix, suffix and a sub-string present in the URL.

Below is a sample configuration using the Path fetch method:

frontend http
 	acl cond1 path -i /app1/abc.html     ##This ACL returns true if the URL path extracted from the request by the path directive matches the value "/app1/abc.html" ##
 	acl cond2 path -i /app2/def.html  
 
 	use_backend be_app1   if cond1	## The backend be_app1 is used whenever the ACL cond1 is TRUE
 	use_backend be_app2   if cond2
 	
backend be_app1
 	balance roundrobin
 	server srv1 XX.YY.ZZ:PP    ## Please replace XX.YY.ZZ with the server IP and PP with the port 
 	server srv2 XX.YY.ZZ:PP
 	
backend be_app2
    balance roundrobin
   	server srv3 XX.YY.ZZ:PP	
 	server srv4 XX.YY.ZZ:PP

Please feel free to revert to this post if you have any queries or any specific requirement pertaining to this answer.

Thanks

1 Like

HAPROXY configuration:

frontend http
bind *:80
acl mpd path_end -i .mpd
acl test hdr(host) test.usp.com https://test.usp.com
use_backend internal if test mpd

backend internal
http-request set-path /vod/
server testirdeto.test.com 172.28.1.16:80 check

Updated the correct comment

Hello Shivraj,

Thank you for the explanation. I am new to HAPROXY and little bit confused.
Below are the details for what i want to achieve with the help of HAPROXY.
I have one webserver for example https://test.com and I want whenever request goes for https://test.com/video/widevine.ism/8.mpd. I require HAPROXY will redirect it to the other server on the specific directory http://test.internal.com/vod/widevine.ism/8.mpd. So that the requested file by the https://test.com server can be served by the http://test.internal.com server.

I tried below settings in HAPROXY configuration file:

bind *:80
acl mpd path_end -i .mpd
acl test hdr(host) test.com https://test.com
use_backend internal if test mpd

backend internal
reqirep set-path var/www/usp/vod/widevine.ism/8.mpd
server test.com 172.28.1.16:80 check

However unfortunately :disappointed_relieved: it is not redirecting
Can you please help me with this.

Hi Priyanshu,

Apologies for the delayed response !!

As i can see in your configuration, the set-path keyword is incorrectly used with reqirep directive. I have gone through the HAProxy documentation but i could not find any mention or usage of set-path keyword with reqirep directive. Not sure if this usage is allowed !

However, based on your requirements, you can achieve the desired redirection using below configuration.

global
        log         127.0.0.1:514 local0
        chroot      /var/lib/haproxy
        pidfile     /var/run/haproxy.pid
        maxconn     4000
        stats timeout 30s
        user    nobody
        group   nobody
        daemon
        stats socket    /var/lib/haproxy/stats  mode    600

defaults
        mode    http
        log     global
        option  httplog
        option  dontlognull
        option  http-server-close
        option  redispatch
        retries 3
        timeout connect 10s
        timeout client  1m
        timeout server  1m
        option forwardfor       except 127.0.0.0/8

frontend ft
        bind www.test.com:80
        bind test.internal.com:80

        acl acld hdr(host) -i www.test.com
        acl aclp path -i /video/widevine.ism/newindex.html

        reqrep ^([^\ ]*)\ /video/(.*) \1\ /vod/\2 if acld aclp
        http-request redirect code 301 location http://test.internal.com/vod/widevine.ism/newindex.html if acld aclp
		## redirect prefix test.internal.com code 301 if acld1 aclp ## This is an alternate that can be used in place of above line. ## 
        default_backend bt2

backend bt2
        balance roundrobin
        cookie SEREVR_ID insert nocache indirect
        server test.example2.com  test.example2.com:80 cookie SERVER1

Please note, you may either use http-request redirect or redirect prefix directive to perform redirection as described in the comment in the configuration file.

As shown in below haproxy.log screenshot, a user with URL www.test.com/video/widevine.ism/newindex.html is redirected to test.internal.com/vod/widevine.ism/newindex.html

Hope this is helpful !

Hello Shivharsh, I stumbled on this post and this something similar to what I am trying to do. I am trying to serve live video channels that I have configured on two backend servers that are configured to serve these requests in the format of following URLs. I am trying to use haproxy as a virtual URL like http://virutalIP/livechannelA/playlist.m3u8 and to forward this to real server http://realserverip/livechannelA/playlist.m3u8. Since the videos are bandwidth intensive, I just need simple round-robin redirects from virtual URL to each physical server. There are close to 10 channels on each server. My main concern is that when the video starts, nothing should pass through haproxy. Is this possible using the method you described above? Sorry, I am new to haproxy and any help is appreciated.

Hello Shivharsh,

Thank you for your response.
Meanwhile I resolved the problem using the apache redirection and it is working fine.
I will try to configure the HAPROXY with the configuration suggested by you and update you if it works fine.

Thanks