I want to use HAProxy for load balancing and failover of an Apache-PHP application hosted on many identical backend servers. Is there any way of detecting a failed node on HAproxy and removing it from the active group of nodes with in the context of a single HTTP request with out returning a 5xx error.

Ideally the server can return a Response Header - "Status: Error" and HAProxy should detect this header and remove that particular node from the active set.

I cannot return 5xx error from my app.

Is such a configuration possible for HAProxy?


A 5xx response is still a response, so by default it would be passed onto the client.

I haven't seen a product that goes to the level you mention out of the box. If none do exist, you need a load balancer that allows you to write a custom post response rule, and for that rule to have access to modify the state of the bad node in the pool and feed the request back into your pool. F5 BigIP's, Riverbed StingRay (Zues) certainly offer the scripting element and node management (f5 e.g. down the bottom). Varnish and Squid although not pure load balancers do offer the ability to modify responses post backend response as well. I'm not sure about their access to the node/pool management though. Note that you would also need to deal with request timeouts in the same manner as 5xx responses.

The page you use to monitor a nodes status would also need be something that thoroughly test's the applications status and can be hit quite frequently

The closest HAProxy looks to come to this is observe <mode> which allows it to react to errors that occur in regular traffic.

I cannot return 5xx error from my app.

At some point it's going to happen, cater for it.


I agree with most of mindthemonkey's comments. Note that you can also block the response and have it replaced with a custom content (using the 502 error), but this is a bit limited (eg: it could be used to send a redirect to make the browser post the request again) and quite dirty.

Why do you want to do this in the first place ? Saying that you "cannot return 5xx" is wrong since you will anyway and the clients have to deal with this too by definition and by the spec. Is this because you're behind a CDN which misbehaves when returning 5xx ? If so, maybe you'd just want to rewrite the output error codes (but be very careful about the codes you use, they all have very specific semantics).

  • There is no reliable way of seding a 500 error from a PHP app. More over my application should indicate that some error has occurred cope with it and move on. Most of the times the 500 error is because of network split in the cluster where the app is unable to access the database from that node. Other web-server nodes might still be able to access the database. So I want to remove that web-server node from the LB and let other nodes do the job as usual. – Praveen Aug 25 '12 at 11:18
  • May be I should instead look into how to return 500 and make things work.... – Praveen Aug 25 '12 at 11:25

Praveen, if you are using at least PHP 5.4 this should help your PHP app:

If you are using PGP 4.3 to 5.3, you can use header() instead (check the man pages for usage).

Technically you can configure HAProxy to do what you ask, but it's probably more appropriate to simply modify your application to behave properly. At that point, HA Proxy should handle it more-or-less automatically.

  • My original idea/requirement is that a node in the cluster can fail but the cluster itself cannot. HAProxy is the face of the cluster and it will be most appropriate for it to retry another node in case of an error for a few times before relaying the error back to the client. – Praveen Jul 3 '14 at 19:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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