37

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.

Tung Nguyen
  • 1,874
  • 3
  • 18
  • 28
rohit369
  • 401
  • 1
  • 4
  • 4

2 Answers2

61

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.

Gooner
  • 1,570
  • 17
  • 20
  • 7
    Don't forget to balance your loadbalancers too. If proxy fails your apps will be unavailable. Check keepalived, can do this job for you with quite simple config – Jan Zahradník Aug 11 '14 at 16:38
  • I have a typical situation, many accounts connect to my load balancer. How can i limit the total number of monthly & daily requests that lands from a particular account.If i have set 200 as the daily threshold for an account say ABC bank, i want to drop all requests beyond that 200, ideally i should also send a "quota over" message to the requestor. Should i do it at the load balancer or should this be taken care at the application instance? – Gaurav Parashar Oct 17 '16 at 05:28
  • 3
    It looks like the backends differ based on the first path component, `path_beg` might be a better test than `path_end` in this case – Jon Wolski Nov 02 '17 at 17:49
2

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