# Redirecting issue

**URL:** <https://discourse.haproxy.org/t/redirecting-issue/7035>\
**Category:** Help!\
**Created:** [October 28, 2021, 9:20am UTC](https://discourse.haproxy.org/t/redirecting-issue/7035 "2021-10-28T09:20:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Deeps](https://avatars.discourse-cdn.com/v4/letter/d/ccd318/32.png) [@Deeps](https://discourse.haproxy.org/u/Deeps)\
**Post date:** [October 28, 2021, 9:20am UTC](https://discourse.haproxy.org/t/redirecting-issue/7035/1 "2021-10-28T09:20:40Z")

</div>

Hi,  
I need help in configuring haproxy. I have an application which is authenticated using keycloak server. Here I am trying to configure 2 keycloak servers for my application for load balancing.

frontend haproxynode  
bind HAPROXYIP:PORT/auth  
mode http  
default\_backend backendnodes

backend backendnodes  
balance roundrobin  
server server1 KEYCLOAK\_SERVER1\_IP:PORT check  
server server2 KEYCLOAK\_SERVER2\_IP:PORT check

I am new in this and this is the configuration I am trying right now. If I am commenting one of the backend servers it works fine. If both the servers are enabled in backend section, request goes to both servers at same time and redirection issue happens when logging into the website. What needs to be done to fix this so that first request goes to server1 and second request goes to server 2. Thanks in advance.

Regards,  
Deeps.

---

<div class="post-metadata">

**Author:** ![lukastribus](https://avatars.discourse-cdn.com/v4/letter/l/7ea924/32.png) [@lukastribus](https://discourse.haproxy.org/u/lukastribus)\
**Post date:** [October 28, 2021, 4:42pm UTC](https://discourse.haproxy.org/t/redirecting-issue/7035/2 "2021-10-28T16:42:39Z")

</div>

> [@Deeps](#):
>
> bind HAPROXYIP:PORT/auth

This is wrong. You can’t specify URI’s here. The syntax is `IP:port`

> [@Deeps](#):
>
> If both the servers are enabled in backend section, request goes to both servers at same time

No. A request either goes to one server or another.

> [@Deeps](#):
>
> redirection issue happens when logging into the website

Caused by load-balancing, most likely, you will to stick a client to a specific server, because server1 doesn’t know anything about the session on server2 and vice versa, so when haproxy load-balances between the two, those servers get confused.

> [@Deeps](#):
>
> What needs to be done to fix this so that first request goes to server1 and second request goes to server 2.

That’s what already happens and is the root cause for your issue.

To fix it, we need to configure haproxy to stop doing this.

Replace `balance roundrobin` with `balance source`, so the clients source IP address is hashed and used for the server selection.

There are other way, like adding cookies for server persistence.

---

<div class="post-metadata">

**Author:** ![Deeps](https://avatars.discourse-cdn.com/v4/letter/d/ccd318/32.png) [@Deeps](https://discourse.haproxy.org/u/Deeps)\
**Post date:** [October 29, 2021, 6:36am UTC](https://discourse.haproxy.org/t/redirecting-issue/7035/3 "2021-10-29T06:36:19Z")

</div>

Thank you Lukas.

---

<div class="post-metadata">

**Author:** ![Deeps](https://avatars.discourse-cdn.com/v4/letter/d/ccd318/32.png) [@Deeps](https://discourse.haproxy.org/u/Deeps)\
**Post date:** [November 6, 2021, 8:48am UTC](https://discourse.haproxy.org/t/redirecting-issue/7035/4 "2021-11-06T08:48:16Z")

</div>

Hi,  
Now I am trying to configure sticky session for my haproxy.

frontend haproxynode  
bind HAPROXYIP:PORT  
mode http  
default\_backend backendnodes

backend backendnodes  
balance roundrobin  
cookie AUTH\_SESSION\_ID prefix nocache  
option forwardfor  
#http-request set-header X-Forwarded-Port %[dst\_port]  
#http-request add-header X-Forwarded-Proto https if { ssl\_fc }  
#option httpchk HEAD / HTTP/1.1\r\nHost:localhost  
server keynd1 KEYCLOAK\_SERVER1\_IP:PORT check cookie keynd1  
server keynd2 KEYCLOAK\_SERVER2\_IP:PORT check cookie keynd2

Since I am working with keycloak servers, I think AUTH\_SESSION\_ID is the cookie that I should use for enabling the sticky sessions. How do we include HTTP headers in haproxy? With this above given configuration I am able to get the keycloak login screen, but getting forbidden error after logging in. How to fix this issue?

Thanks in advance,  
Deeps.
