rohit369
rohit369

Reputation: 411

HAProxy - URL Based routing with load balancing

I am new to HAProxy and I have a question about HAProxy configuration which helps me make a key decision in taking the right approach. This will greatly help me deciding the architecture.

I have 3 apps. Let's say app1, app2, app3.

Each app is differentiated by the urls as follows:

www.example.com/app1/123 -> app1
www.example.com/app2/123 -> app2
www.example.com/app3/123 -> app3

I am planning to have 2 instances of each app in 2 different regions:

Region 1 - app1, app2, app3
Region 2 - app1, app2, app3

I see 2 methods to configure this but I am not sure which is the best practice here:

I am not sure if Method 2 is supported in haproxy. Any ideas or suggested is greatly appreciated. Please put some light.

Upvotes: 38

Views: 76651

Answers (2)

Wirat Leenavonganan
Wirat Leenavonganan

Reputation: 119

Using acl in HAProxy to separate route for each application. You can use path_end or path_beg to match the path. Anyway, if 'd like to change request path to backend, using 'http-request set-uri' and using reg-sub pattern.


backend be_images
        balance roundrobin  
        http-request set-uri '%[path,regsub(^/images/,/static/images,g)]'
        server srv1 127.0.0.1:8001

Upvotes: 2

Gooner
Gooner

Reputation: 1590

You can segregate requests based on URL and load balance with a single HAProxy server. Your configuration will have something like this:

frontend http
acl app1 path_end -i /app1/123 #matches path ending with "/app/123"
acl app2 path_end -i /app2/123 
acl app3 path_end -i /app3/123 


use_backend srvs_app1    if app1
use_backend srvs_app2    if app2
use_backend srvs_app3    if app3

backend srvs_app1 #backend that lists your servers. Use a balancing algorithm as per your need.
   balance roundrobin 
   server host1 REGION1_HOST_FOR_APP1:PORT 
   server host2 REGION2_HOST_FOR_APP1:PORT

backend srvs_app2
   balance roundrobin
   server host1 REGION1_HOST_FOR_APP2:PORT 
   server host2 REGION2_HOST_FOR_APP2:PORT

backend srvs_app3
   balance roundrobin
   server host1 REGION1_HOST_FOR_APP3:PORT 
   server host2 REGION2_HOST_FOR_APP3:PORT

More information can be found on the homepage.

Upvotes: 62

Related Questions