# Haproxy failover single backup server for several nodes

**URL:** https://discourse.haproxy.org/t/haproxy-failover-single-backup-server-for-several-nodes/7716
**Category:** Help!
**Created:** [June 3, 2022, 8:05am UTC](https://discourse.haproxy.org/t/haproxy-failover-single-backup-server-for-several-nodes/7716 "2022-06-03T08:05:54Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Liso](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.haproxy.org/liso/32/1711_2.png) [@Liso](https://discourse.haproxy.org/u/Liso)
#### Post date: [June 3, 2022, 8:05am UTC](https://discourse.haproxy.org/t/haproxy-failover-single-backup-server-for-several-nodes/7716/1 "2022-06-03T08:05:54Z")

</div>

I have several small server and one big server that contain mirror of that several small server. Let’s call it Server A, Server B, Server C and Server D for group of small server, and the big server will have Server X as name.

I’m drawing this diagram to explain the setup.

 ![Haproxy-setup(2)](https://us1.discourse-cdn.com/flex016/uploads/haproxy/original/2X/9/968afc7731577af3a9c9939bd2f66e2a3fa75b58.png)

I want to redirect user to Server X if one of Server A to D is down/unreachable, once the server is gone up the user would have been redirected back to their corresponding server. Below is an example of desired scenario.

- John search up `www.example.com` hosted on Server B.
- Haproxy redirect John to Server B.
- John is doing something on `www.example.com` on server B.
- Server B is down ! John got redirected to Server X.
- John is doing something on `www.example.com` on server X.
- Server B is up ! John got redirected back to server B from Server X.

I want to do the same setup with all other server (Server A to D), hence I only needed a failover and not load balancer.

I’m pretty sure this is possible with haproxy, but I haven’t find a way to do this setup. Can anyone who knowledgeable give me a way to perform this setup.

I hope my point is clear, TIA

---

<div class="post-metadata">

### Author: ![Liso](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.haproxy.org/liso/32/1711_2.png) [@Liso](https://discourse.haproxy.org/u/Liso)
#### Post date: [June 6, 2022, 4:07am UTC](https://discourse.haproxy.org/t/haproxy-failover-single-backup-server-for-several-nodes/7716/2 "2022-06-06T04:07:55Z")

</div>

Anyone can help ?

---

<div class="post-metadata">

### Author: ![rhada](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.haproxy.org/rhada/32/585_2.png) [@rhada](https://discourse.haproxy.org/u/rhada)
#### Post date: [June 7, 2022, 8:56am UTC](https://discourse.haproxy.org/t/haproxy-failover-single-backup-server-for-several-nodes/7716/3 "2022-06-07T08:56:23Z")

</div>

Hello,

In your backend servers definition, you can use Server X as backup :

```auto
backend backend-A
    server server-A server-a:80 check
    server server-X server-x:80 check backup

```

This way, if server A goes down, server X wil be used. When server A goes UP, requests will be sent to it.

---

<div class="post-metadata">

### Author: ![Liso](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.haproxy.org/liso/32/1711_2.png) [@Liso](https://discourse.haproxy.org/u/Liso)
#### Post date: [June 7, 2022, 9:13am UTC](https://discourse.haproxy.org/t/haproxy-failover-single-backup-server-for-several-nodes/7716/4 "2022-06-07T09:13:29Z")

</div>

What about another server ? Do I need to create different backend for each server ?

---

<div class="post-metadata">

### Author: ![Gabrieltz](https://avatars.discourse-cdn.com/v4/letter/g/5daacb/32.png) [@Gabrieltz](https://discourse.haproxy.org/u/Gabrieltz)
#### Post date: [June 8, 2022, 2:45pm UTC](https://discourse.haproxy.org/t/haproxy-failover-single-backup-server-for-several-nodes/7716/5 "2022-06-08T14:45:27Z")

</div>

hello @Liso ,  
I think you need to create two backends , one with the four servers ( A-D ) and one with only server X inside the backend blocks like so:

```auto
backend fourofthem
    mode http
    balance roundrobin
    server A ip-for-A check
    server B ip-for-B check
    server C ip-for-C check
    server D ip-for-D check

backend justx
    mode http
    balance roundrobin
    server X ip-for-X check

```

Now , in your frontend you may specify an acl that checks for the number of servers in backend “fourofthem” and redirect your traffic to backend “justx” if the number of servers is less than four  
and justx is alive.

The frontend could look like this:

```auto
frontend thefrontend
    listen....
    acl atleastonedown nbsrv(fourofthem) lt 4
    acl x_is_still_alive nbsrv(justx) ge 1
    use_backend justx if atleastonedown x_is_still_alive
    default_backend fourofthem

```

As soon as the one backend of “fourofthem” goes down the first acl will return true and if justx is alive as well , it will get the traffic. Otherwise all traffic will end in your default backend “fourofthem”.

For reference nbsrv() receives a string input argument, which is the backend name and returns the number of alive servers in that backend.

I hope this solves your problem

---

<div class="post-metadata">

### Author: ![Markus](https://avatars.discourse-cdn.com/v4/letter/m/b782af/32.png) [@Markus](https://discourse.haproxy.org/u/Markus)
#### Post date: [June 9, 2022, 7:32am UTC](https://discourse.haproxy.org/t/haproxy-failover-single-backup-server-for-several-nodes/7716/6 "2022-06-09T07:32:38Z")

</div>

don’t think that this will work. as soon one of the four servers is down ALL traffic will be routed to serverX.  
you can setup a backup server. i am not sure if the backup server will receive requests as soon one of the main servers is down. and i am not sure if the stickyness will route the requests back to the original server (user-request to serverB, serverB is down, user-request to serverX, as soon as serveB is up again the user is back on serverB), i could image that then the load-balancing routing is done new. maybe you have to check this with a sample setup?

---

<div class="post-metadata">

### Author: ![Gabrieltz](https://avatars.discourse-cdn.com/v4/letter/g/5daacb/32.png) [@Gabrieltz](https://discourse.haproxy.org/u/Gabrieltz)
#### Post date: [June 10, 2022, 1:06pm UTC](https://discourse.haproxy.org/t/haproxy-failover-single-backup-server-for-several-nodes/7716/7 "2022-06-10T13:06:59Z")

</div>

I misunderstood the goal, i thought @Liso wanted to send all traffic to X if any one server failed.

I that case then in my example if we add all 4 servers AND server X in justx backend it will work as expected.

If any server in the default backend fails , the frontend will send the traffic to justx backend.  
server B would be failed, so checks for it fail , so all requests are servers by A C D and X.

```auto
backend justx
    mode http
    balance roundrobin
    server X ip-for-X check
    server A ip-for-A check
    server B ip-for-B check
    server C ip-for-C check
    server D ip-for-D check

```
