Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using domain instead of IP in haproxy backend

I have 2 virtual host

app.example.com:80 on ip address xxx.xxx.xxx.xxx

app2.example.com:80 on ip address yyy.yyy.yyy.yyy

my haproxy ipaddress is sss.sss.sss.sss

This is haproxy configuration :

global
   log 127.0.0.1 local0 notice
   maxconn 2000
   user haproxy
   group haproxy

defaults
  log     global
  mode    http
  option  httplog
  option  dontlognull
  retries 3
  option redispatch
  timeout connect  5000
  timeout client  10000
  timeout server  10000

frontend www-http
    mode http
    bind *:80
    default_backend appname
    stats enable
    stats uri /haproxy?stats
    stats auth admin:password
    stats show-node

backend appname
  balance roundrobin
  option httpclose
  option forwardfor
  server lamp1 app.example.com:80 check
  server lamp2 app2.example.com:80 check

When trying to access using haproxy ipaddress, web browser returns xampp dashboard instead of backend content.

How can i make haproxy to redirect to backend content?

like image 981
Lundu Harianja Avatar asked Sep 25 '22 23:09

Lundu Harianja


1 Answers

I do believe that functionality is now available in 1.6, http://blog.haproxy.com/2015/10/14/whats-new-in-haproxy-1-6/

what you have configured will simply loadbalance the request between those to instances :

server lamp1 app.example.com:80 check
server lamp2 app2.example.com:80 check

if they are 2 seperate apps rather try:

frontend www-http
mode http
bind sss.sss.sss.sss:80
stats enable
stats uri /haproxy?stats
stats auth admin:password
stats show-node

acl app01 hdr(Host) -i app.example.com
acl app02 hdr(Host) -i app02.example.com

use_backend app01 if app01
use_backend app02 if app02

backend app01
balance roundrobin
option httpclose
option forwardfor
server lamp1 xxx.xxx.xxx.xxx:80 check

backend app02
balance roundrobin
option httpclose
option forwardfor
server lamp2 yyy.yyy.yyy.yyy:80 check

If you now hit your haproxy with app.example.com you will be forwarded to lamp1 and app2.example.com will take you to lamp2

if you want to forward everything to the ip to the backend and dont care for extra matching and mapping then id use a straight listen, instead of a frontend :

 listen SOMENAME sss.sss.sss.sss:80
 balance leastconn
 mode http
 server lamp1 xxx.xxx.xxx.xxx:80
 server lamp2 yyy.yyy.yyy.yyy:80
like image 172
Louis Kriek Avatar answered Nov 26 '22 21:11

Louis Kriek