Setting a custom header on a proxied request

Maxim Dounin mdounin at mdounin.ru
Wed Oct 14 16:32:51 UTC 2015


Hello!

On Mon, Oct 12, 2015 at 02:12:47PM +0000, Maxime Henrion wrote:

> Hi all,
> 
> 
> I'm currently writing an nginx module that sends a number of 
> subrequests to upstream servers based on application logic, and 
> then aggregates the results and send them back. That part 
> currently works just fine, thanks to the help I was given here 
> in the past.
> 
> However, I am now trying to add a custom header to the request 
> that is being proxied and currently failing at it. For various 
> technical reasons, I need to add a header that contains the IP 
> address and port (from the point of view of nginx) of the 
> upstream server currently being contacted. In order to get at 
> that address, I have hooked my module into the peer selection 
> mechanism, much like the keepalive, ip_hash modules and others 
> do. So far so good, I get at that address and can log it just 
> fine. However, I can't seem to be able to add it as a header to 
> my request.

[...]

>         h = ngx_list_push(&r->headers_in.headers);
>         if (h == NULL) {
>                 return NGX_ERROR;
>         }
>         h->hash = 1;
>         ngx_str_set(&h->key, "X-Shard-Addr");
>         h->value.data = ngx_pstrdup(r->pool, &peeraddr);
>         if (h->value.data == NULL) {
>                 return NGX_ERROR;
>         }
> 
>         h->value.len = peeraddr.len;
> 
>         return NGX_OK;
> }
> 
> So after getting at the actual address we're forwarding our 
> request to, I try to add a custom header to r->headers_in, on 
> the request object that I saved in my peer data.
> 
> I suspect I'm not actually dealing with the correct request 
> here, because no matter what I try, I can't get this code to 
> actually alter the headers of the forwarded request in any way.

What you are trying to do is wrong.  You shouldn't modify original 
request headers - they are expected to be from original request, 
and arbitrary modifications will cause problems with later 
processing.  Instead, consider using the proxy_set_header 
directive (http://nginx.org/r/proxy_set_header) to add headers to 
requests to upstream servers.

Additionally, this is not going to work as you are doing it too 
late.  You are trying to add headers from balancing function, but 
balancing happens when a request to upstream is already created.  
(And re-balancing due to proxy_next_upstream may select a 
different upstream server for the same request.)

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



More information about the nginx-devel mailing list