Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I'm testing running HAProxy as a dedicated load balancer behind Apache 2.2, replacing our current configuration where we use Apache's load balancer. In our current, Apache only, set-up if all the backend (origin) servers are down Apache will serve a 503 service unavailable message. With HAProxy I get a 502 bad gateway response.

I'm using a simple reverse proxy rewrite rule in Apache

RewriteRule ^/(.*) http://127.0.0.1:8000/$1 [last,proxy]

In HAProxy I have the following (running in default tcp mode)

defaults
    log             global
    option          tcp-smart-accept
    timeout connect 7s
    timeout client  60s
    timeout queue   120s
    timeout server  60s

listen my_server 127.0.0.1:8000
    balance leastconn
    server backend1 127.0.0.1:8001 check observe layer4 maxconn 2
    server backend1 127.0.0.1:8001 check observe layer4 maxconn 2

Testing connecting directly to the load balancer when the backend servers are down:

[root@dev ~]# wget http://127.0.0.1:8000/ test.html
--2012-05-28 11:45:28--  http://127.0.0.1:8000/
Connecting to 127.0.0.1:8000... connected.
HTTP request sent, awaiting response... No data received.

So presumably this is down to the fact that HAProxy accepts the connection and then closes it.

share|improve this question
    
Why are you putting apache in front of HAProxy? –  coredump May 28 '12 at 11:01
1  
We already have Apache but aren't satisfied with its load balancer and are switching to HAProxy instead. Apache is also serving as our SSL endpoint, cache server as well as a reverse proxy. We have years worth of rewrite rules and 10s of sites so getting rid of Apache completely is not an option. –  scarba05 May 28 '12 at 13:25

2 Answers 2

In TCP mode, haproxy does not emit any status code so the only remaining point clearly is apache. I think that it's simply because haproxy accepts then closes the connection that makes apache return a 502, which is expected.

So the behaviour you're observing is correct. Anyway it's generally better to work in HTTP mode. I also suggest that you enable "option httplog" which will provide you with very detailed logs, and "option http-server-close" to take advantage of apache's ability to maintain keep-alive with haproxy, it will significantly reduce local source port consumption on the machine.

share|improve this answer
up vote 0 down vote accepted

I couldn't get this working in tcp mode but if you switch to http mode then you do get the 503

defaults
    mode http
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.