# Choosing source-address for upstream connections

**URL:** <https://www.ruby-forum.com/t/choosing-source-address-for-upstream-connections/234897>\
**Category:** NGINX\
**Created:** [May 17, 2013, 2:57pm UTC](https://www.ruby-forum.com/t/choosing-source-address-for-upstream-connections/234897 "2013-05-17T14:57:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![addis\_a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c4cdca/32.png) [@addis\_a](https://www.ruby-forum.com/u/addis_a)\
**Post date:** [May 17, 2013, 2:57pm UTC](https://www.ruby-forum.com/t/choosing-source-address-for-upstream-connections/234897/1 "2013-05-17T14:57:34Z")

</div>

Is there a way to tell nginx to use a specific address when talking to  
an upstream?

I would like to do something like:

upstream bla {  
server [D0C::1234]:8080 src [D0C::beaf];  
server 1.2.3.4:8080 src 1.2.3.5;  
server upstream.example.tld src site.example.tld;  
}

My system has multiple local addresses and just one of them should be  
used to contact the given upstream.

If host-name is provided the source address should be chosen of the  
same address family as the upstream address (so it’s not necessary to  
explicitly state IP addresses and duplicate server entries for IPv4  
and IPv6).

It’s possible to do it via “src” attribute of routes though it would be  
more clean to do it on the application side.  
e.g.  
ip route add D0C::1234/128 src D0C::beaf ethX  
ip route add 1.2.3.4/32 src 1.2.3.5

Bruno

---

<div class="post-metadata">

**Author:** ![SSBruno\_PrSSmontSS\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b4bc9f/32.png) [@SSBruno\_PrSSmontSS\_S](https://www.ruby-forum.com/u/SSBruno_PrSSmontSS_S)\
**Post date:** [May 17, 2013, 3:18pm UTC](https://www.ruby-forum.com/t/choosing-source-address-for-upstream-connections/234897/2 "2013-05-17T15:18:02Z")

</div>

On Fri, May 17, 2013 at 02:57:00PM +0200, Bruno Prmont wrote:

> It’s possible to do it via “src” attribute of routes though it would be  
> more clean to do it on the application side.  
> e.g.  
> ip route add D0C::1234/128 src D0C::beaf ethX  
> ip route add 1.2.3.4/32 src 1.2.3.5

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

This is the only option so far. Per-server per-upstream source address  
selection isn’t currently possible.

---

<div class="post-metadata">

**Author:** ![SSBruno\_PrSSmontSS\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b4bc9f/32.png) [@SSBruno\_PrSSmontSS\_S](https://www.ruby-forum.com/u/SSBruno_PrSSmontSS_S)\
**Post date:** [May 17, 2013, 3:28pm UTC](https://www.ruby-forum.com/t/choosing-source-address-for-upstream-connections/234897/3 "2013-05-17T15:28:33Z")

</div>

On Fri, 17 May 2013 17:17:20 +0400 Ruslan E. wrote:

> > }
> 
> selection isn’t currently possible.  
> How does that one behave with regard to IPv4 versus IPv6?

The documentation says “address” which I guess can be either IPv4 or  
IPv6 but not both so the right one gets choosen depending on the  
destination address format? Or can I write it twice, once with IPv4  
addr, once with IPv6 addr (ideally once with a hostname that gets  
resolved as appropriate).

As it can be set on per-server or per-location level it would be  
sufficient for me.

Bruno
