Welcome! Log In Create A New Profile

Advanced

Re: Setting a custom header on a proxied request

Maxim Dounin
October 14, 2015 12:34PM
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/

_______________________________________________
nginx-devel mailing list
nginx-devel@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx-devel
Subject Author Views Posted

Setting a custom header on a proxied request

Maxime Henrion 593 October 12, 2015 10:14AM

Re: Setting a custom header on a proxied request

Andrew Hutchings 234 October 12, 2015 12:06PM

RE: Setting a custom header on a proxied request

Maxime Henrion 278 October 12, 2015 01:06PM

Re: Setting a custom header on a proxied request

Sorin Manole 275 October 12, 2015 02:08PM

Re: Setting a custom header on a proxied request

Maxim Dounin 199 October 14, 2015 12:36PM

RE: Setting a custom header on a proxied request

Maxime Henrion 214 October 14, 2015 12:14PM

Re: Setting a custom header on a proxied request

Maxim Dounin 222 October 14, 2015 12:34PM

RE: Setting a custom header on a proxied request

Maxime Henrion 254 October 14, 2015 12:58PM

Re: Setting a custom header on a proxied request

Maxim Dounin 248 October 14, 2015 01:10PM



Sorry, you do not have permission to post/reply in this forum.

Online Users

Guests: 232
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready