HAproxy redirect all HTTP requests from single frontend to single backend (multiple ports)

11,498

You can do this by omitting the :port on the server line

frontend public_http
    # Listen on port 80 to 1024, included
    bind :80-1024
    # Listen on ports 8088, 8080, 8000
    bind :8088,:8080,:8000
    mode http
    # Define ACLs for each domain
    acl acl_webtest  hdr(host) -i -f /etc/haproxy/acls/webtest
    use_backend back_web_test if acl_webtest

backend back_web_test
    mode http
    balance roundrobin  # Load Balancing algorithm
    option httpchk
    option forwardfor
    server webtest 192.168.0.123 weight 1 port 80 maxconn 512 check

With this configuration, the backend connection will be made to the same port the client connected to. The separate port keyword on the server line is used for health checks.

Share:
11,498

Related videos on Youtube

CROSP
Author by

CROSP

Updated on September 18, 2022

Comments

  • CROSP
    CROSP over 1 year

    I have haproxy installed and it works just fine, currently I have configuration that looks as follows

    frontend public_http
            # Listen on port 80
            bind *:80
            mode http
            # Define ACLs for each domain
            acl acl_webtest  hdr(host) -i -f /etc/haproxy/acls/webtest
            use_backend back_web_test if acl_webtest
    
    backend back_web_test
            mode http
            balance roundrobin  # Load Balancing algorithm
            option httpchk
            option forwardfor
            server webtest 192.168.0.123:80 weight 1 maxconn 512 check
    

    As you can see I am redirecting incoming HTTP traffic to the backend on the same port (80 default for HTTP).

    But my question :
    Is it possible to have single frontnend without bunch of bind *:<port> statements and redirect to a single backend to the same <port>. BUT only HTTP traffic. I know that default port for HTTP is 80, but we are able to send HTTP request to any port.

    It looks like TCP proxy, but I need to redirect to a backend based on domain, and TCP layer (OSI 4) doesn't know anything about domain.

    So I hope, I've described my problem clearly.

    I would be grateful for any help.

    • Michael - sqlbot
      Michael - sqlbot over 7 years
      You are saying redirect, but you almost certainly mean to say forward. When you redirect, the proxy sends a 3XX HTTP response to the browser, along with a Location: response header, the browser changes its address bar, and connects to the redirect target. TCP connections don't redirect.