# Sending basic auth to "backend" servers

**URL:** <https://www.ruby-forum.com/t/sending-basic-auth-to-backend-servers/244684>\
**Category:** NGINX\
**Created:** [May 25, 2016, 10:03pm UTC](https://www.ruby-forum.com/t/sending-basic-auth-to-backend-servers/244684 "2016-05-25T22:03:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sadaf\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@Sadaf\_N](https://www.ruby-forum.com/u/Sadaf_N)\
**Post date:** [May 25, 2016, 10:03pm UTC](https://www.ruby-forum.com/t/sending-basic-auth-to-backend-servers/244684/1 "2016-05-25T22:03:14Z")

</div>

Hey everyone

I’m trying to achieve something a little unique (have read a LOT of  
documentation and posts)

I want to use ngnix as a LB to a handful of squid servers I have to  
distribute http requests on a round robin basis.

Each squid web server in the “backend” accepts a unique username and  
password.

Would anyone be able to point me in the right direction config wise on  
how  
to define a unique user/pass for each server in the load balanced pool?

Many thanks  
JR

Posted at Nginx Forum:

> **[Sending basic auth to "backend" servers](https://forum.nginx.org/read.php?2,267156,267156#msg-267156)**
>
> Hey everyone I'm trying to achieve something a little unique (have read a LOT of documentation and posts) I want to use ngnix as a LB to a handful of squid servers I have to distribute http requests on a round robin basis. Each squid web server in...

---

<div class="post-metadata">

**Author:** ![jrhodes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/53a042/32.png) [@jrhodes](https://www.ruby-forum.com/u/jrhodes)\
**Post date:** [May 26, 2016, 3:25pm UTC](https://www.ruby-forum.com/t/sending-basic-auth-to-backend-servers/244684/2 "2016-05-26T15:25:57Z")

</div>

Hello!

On Wed, May 25, 2016 at 04:02:46PM -0400, jrhodes wrote:

> Would anyone be able to point me in the right direction config wise on how  
> to define a unique user/pass for each server in the load balanced pool?

Normal logic of load balancing in nginx assumes the same request can  
be sent to multiple backend servers, and therefore it won’t work  
for you - as you need to construct unique request to each backend,  
with it’s own Authorization header.

To do what you want you may try distributing requests “by hand”,  
e.g., using the split\_clients module:

```
split_client $remote_addr $backend {
    50% backend1.example.com;
    * backend2.example.com;
}

map $backend $backend_auth {
    backend1.example.com QWxhZGRpbjpvcGVuIHNlc2FtZQ==;
    backend2.example.com QWxhZGRpbjpvcGVuIHNlc2FtZQ==;
}

server {
    ...

    location / {
        proxy_pass http://$backend;
        proxy_set_header Authorization "Basic $backend_auth";
    }
}

```

More information can be found in the documentation here:

> **[Module ngx\_http\_split\_clients\_module](http://nginx.org/en/docs/http/ngx_http_split_clients_module.html)**

> **[Module ngx\_http\_map\_module](http://nginx.org/en/docs/http/ngx_http_map_module.html)**

> **[Module ngx\_http\_proxy\_module](http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass)**
>
> The ngx\_http\_proxy\_module module allows passing requests to another server.

–  
Maxim D.

> **[nginx](http://nginx.org/)**

---

<div class="post-metadata">

**Author:** ![jrhodes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/53a042/32.png) [@jrhodes](https://www.ruby-forum.com/u/jrhodes)\
**Post date:** [May 26, 2016, 9:07pm UTC](https://www.ruby-forum.com/t/sending-basic-auth-to-backend-servers/244684/3 "2016-05-26T21:07:41Z")

</div>

Maxim, thanks so much. I’ll read those parts!

Posted at Nginx Forum:

> **[Re: Sending basic auth to "backend" servers](https://forum.nginx.org/read.php?2,267156,267184#msg-267184)**
>
> Hey everyone I'm trying to achieve something a little unique (have read a LOT of documentation and posts) I want to use ngnix as a LB to a handful of squid servers I have to distribute http requests on a round robin basis. Each squid web server in...

---

<div class="post-metadata">

**Author:** ![jrhodes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/53a042/32.png) [@jrhodes](https://www.ruby-forum.com/u/jrhodes)\
**Post date:** [May 27, 2016, 4:14pm UTC](https://www.ruby-forum.com/t/sending-basic-auth-to-backend-servers/244684/4 "2016-05-27T16:14:58Z")

</div>

Just one follow up question after some testing. Is there a way to split  
on a  
per request basis?

So say 10 backend servers each with unique authorisation headers set up,  
then have each single incoming request RR to each:

Incoming http request 1 → backend1  
Incoming http request 2 → backend2  
Incoming http request 3 → backend3

And so on…

Thanks!  
JR

Posted at Nginx Forum:

> **[Re: Sending basic auth to "backend" servers](https://forum.nginx.org/read.php?2,267156,267197#msg-267197)**
>
> Hey everyone I'm trying to achieve something a little unique (have read a LOT of documentation and posts) I want to use ngnix as a LB to a handful of squid servers I have to distribute http requests on a round robin basis. Each squid web server in...
