Haproxy redirect ip request to ip/path

haproxy

I have one haproxy node and 3 application server (tomcat) behind it. Users must open http://10.0.0.1 (by IP address) and then be redirected to http://10.0.0.2/app or http://10.0.0.3/app or http://10.0.0.4/app.

Current haproxy config:

global
    log 127.0.0.1   local0
    log 127.0.0.1   local1 notice
    chroot /var/lib/haproxy
    user haproxy
    group haproxy
    daemon
    maxconn 50000
defaults
    log     global
    mode    http
    option  httplog
    option  dontlognull
#       option httpchk OPTIONS * HTTP/1.1\r\nHost:\ www
    retries 3
    redispatch
    contimeout 1s
    clitimeout 2s
    srvtimeout 3s
    errorfile 400 /etc/haproxy/errors/400.http
    errorfile 403 /etc/haproxy/errors/403.http
    errorfile 408 /etc/haproxy/errors/408.http
    errorfile 500 /etc/haproxy/errors/500.http
    errorfile 502 /etc/haproxy/errors/502.http
    errorfile 503 /etc/haproxy/errors/503.http
    errorfile 504 /etc/haproxy/errors/504.http
    listen stats :1941
    mode http
    stats enable
    stats hide-version
    stats realm Haproxy\ Statistics
    stats uri /
    stats auth xxx:xxx
listen appfarm 10.0.0.1:80
    mode http
    stats enable
    stats auth xxx:xxx
    balance source
    hash-type consistent
    option httpclose
    option forwardfor
#   option httpchk HEAD /check.txt HTTP/1.0
    server app01 10.0.0.2:8080/app check
    server app02 10.0.0.3:8080/app check
    server app03 10.0.0.4:8080/app check

With that config I got tomcat welcome page from address 10.0.0.1:8080, without /app in address.

Best Answer

You cannot add a path on the server line like that, you will want to use a redirect in your listen block above the server lines:

redirect location /app if { url / }

and convert your server lines to

server app01 10.0.0.2:8080 check

See http://cbonte.github.io/haproxy-dconv/1.6/configuration.html#redirect for further explanation/examples, and note that this example is using anonymous ACLs which can get kludgy with bigger configs.