Configure virtualhost on haproxy

hallo, can someone help me? I have problem with multiple website that I hosted on my backend server, I have 2 backend server as follow :

  1. server1.local 192.168.10.100
  2. server2.local 192.168.11.100

and my haproxy server is : server3.local 192.168.12.10

my dirrectory web I put it on both backend server :

  1. server1 /var/www/html/nextcloud
  2. server2 /var/www/html/nextcloud

I wanto configure load balancing and HA in HAProxy server, but not working, my haproxy.cfg is as follow :

frontend LB
bind *:80
reqadd X-Forwarded-Proto:\ http
#default_backend LB

Define hosts

acl host_cacti hdr(host) -i dakusoft.cacti
acl host_osticket hdr(host) -i dakusoft.osticket
acl host_nextcloud hdr(host) -i dakusoft.nextcloud

figure out which one to use

use_backend cacti_cluster if host_cacti
use_backend osticket_cluster if host_osticket
use_backend nextcloud_cluster if host_nextcloud

backend cacti_cluster
balance leastconn
option httpclose
rspadd X-Frame-Options:\ SAMEORIGIN
option forwardfor
cookie JSESSIONID prefix
server server1 server1.local:80 cookie server1 check
#server server2 server2.local:80 cookie server2 check

backend nextcloud_cluster
balance leastconn
rspadd X-Frame-Options:\ SAMEORIGIN
option httpclose
option forwardfor
cookie JSESSIONID prefix
server server1 server1.local:80 cookie server1 check
server server2 server2.local:80 cookie server2 check

and this is virtualhost configuration on backend that I created on both of backend server

First of all, please note that you can use Markdown to properly format the configuration as a snippet. (As it stands now, it is extremely hard to follow.)

Now regarding the actual setup, it is a little bit unclear if both backend servers host all the three domains? (If so, then you don’t need three backends, just one for the whole cluster;
else indeed you need three backends in which you put only the relevant servers.)

Regardless of the above observation, the HAProxy configuration seems OK (at a first glance), thus I would say it might be a configuration issue on the backend server themselves. (Do they properly respond to these domains when you put their IP’s in /etc/hosts for the target domains?)

thanks for your reply. yes, both backend servers host all the three domains… all my backend server has reachable each one other, I already configure it on etc/hosts. and now the problem has been solved… the problem is in the “cookie JSESSIONID prefix” line which I added in the backend configuration.I deleted this line and restart haproxy and now working properly with virtual host. you said only 1 backend for whole cluster, configuratio0n that I made above is from tutorials on internet for configuration multiple domain using ACL.can you to give me example config for for only 1 backend for whole cluster in multiple domain?

If all of the servers have the same role (i.e. they can replace one-another), and if you don’t serve other domains with this HAProxy instance (except those already handled by the current servers), then you can leave only one of those backends, and use it with default_backend; moreover you don’t need any ACL statements.

Basically you’re using HAProxy just as a load-balancer. (As opposed to an “HTTP-router” as you were with those ACL’s.)

(The tutorials you’ve seen had perhaps different servers in each backend. Or perhaps they did something more complex, like “mangling” HTTP traffic in each backend based differently for each domain.)

In the end it doesn’t matter much if you split the domains in three backends or a single one, except:

  • having different backends could allow you to gather some “primitive” statistics about the load;
  • if you consider fiddling with the various load fine-tuning parameters (like queue length, max concurrent sessions, etc.), note that HAProxy sees each server in the three backends as different ones, thus it might “lie” about the total capacity you have at your disposal;