upstream host name or address

Maxim Dounin mdounin at mdounin.ru
Mon Jun 20 01:39:58 MSD 2011


Hello!

On Sun, Jun 19, 2011 at 07:26:58AM -0400, torajx wrote:

> hi,
> is it possible to find upstream host which serves as proxy for a
> connection;
> i know that $upstream_addr is the key ; but i need to set the address
> with set_proxy_headdr as host
> but $upstream_addr return address and port together
> 
> upstream test
> {
> ip_hash;
> 192.168.1.1;
> 192.168.1.2;
> }
> 
> location /
> {
> proxy_set_header Host $upstream_addr;  ---------> fail cause it set for
> example 192.168.1.1:80 but i need 192.168.1.1
> proxy_pass http://test;
> }
> 
> 
> any suggestion ?

Request is created only once and you can't set different headers 
when working with different servers in upstream.  Servers in 
upstream block must accept identical requests.

(Moreover, $upstream_addr isn't an address of upstream server nginx is
talking to, but a list of servers nginx have talked to during 
handling of a request.  And usually it won't contain anything 
while creating request to upstream.)

If you really need different requests to different backend servers 
you'll have to use different proxy_pass'es for that (or use 
variables).  E.g. in 0.9.6+ you may do something like this:

    map $remote_addr $backend {
        default          "should not happen";
        ~[02468]\.\d+$   192.168.1.1;
        ~[13579]\.\d+$   192.168.1.2;
    }

    location / {
        proxy_pass http://$backend;
    }

(no need for proxy_set_header here, as $proxy_host will resolve to 
the backend's ip address and default "proxy_set_header Host 
$proxy_host;" will do what you want)

But note that recommended aproach is to actually configure 
identical backend hosts, not adapt requests for each one.

Maxim Dounin



More information about the nginx mailing list