Setting a custom header on a proxied request

Maxim Dounin mdounin at mdounin.ru
Wed Oct 14 17:09:27 UTC 2015


Hello!

On Wed, Oct 14, 2015 at 04:56:35PM +0000, Maxime Henrion wrote:

> Hello Maxim!
> 
> Thank you again for your answer.
> 
> I have read more about the upstream code as well as the the 
> proxy module code, and now realize that headers are being 
> serialized into a buffer a chain within the create_request 
> callback, and that peer selection can happen after that.
> 
> I cannot however use proxy_set_header in this particular case, 
> as I have no variable to use that would expand to the desired 
> value (the actual IP address of the peer currently being 
> contacted). To the best of my knowledge, the $upstream_addr 
> variable is closest to what I need, except that it contains all 
> the upstream addresses that were contacted during request 
> processing.
> 
> Would you have any idea on how best to approach this problem? I 
> was considering implementing a full-blown upstream module, but I 
> have no evidence that the way the ngx_http_upstream.c code works 
> is compatible with a scheme where headers would potentially need 
> to be changed each time we select a new peer.

What you are trying to do is not something easily doable with 
nginx upstream module.  It's designed to reuse the same request 
while connecting to multiple upstream servers from an upstream{} 
block, and it considers all upstream servers in a block to be 
identical - much like it happens when you are using a DNS name 
with multiple A records.

If you want requests to different servers be different, I would 
rather recommend using different upstream blocks and balancing 
requests between them using some external means - e.g., by using 
different locations or by using a proxy_pass with a variable.

-- 
Maxim Dounin
http://nginx.org/



More information about the nginx-devel mailing list