HAProxy - URL Based routing with load balancing

rohit369 picture rohit369 · Dec 16, 2013 · Viewed 61.7k times · Source

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:

  • Method 1: Have HAProxy1 to first differentiate the requests using the url patterns. Requests from HAProxy1 will be routed to another HAProxy server set up individual apps (3 HAProxy servers in this case) for load balancing.

  • Method 2: Have one great HAProxy server which does the both as stated in method 1. That is, have configuration to segregate the requests depending on the url and then pass each request through individual filter like things set up for each app for load balancing.

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

Answer

Gooner picture Gooner · Dec 17, 2013

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.