Restrictions to modifying request->headers_in.headers in NGX_HTTP_PREACCESS_PHASE?

Eran Kornblau eran.kornblau at kaltura.com
Sat Mar 31 14:37:56 UTC 2018


> -----Original Message-----
> From: nginx-devel [mailto:nginx-devel-bounces at nginx.org] On Behalf Of Ryan Burn
> Sent: Friday, March 30, 2018 5:30 PM
> To: nginx-devel at nginx.org; robert at cryptobells.com
> Subject: Re: Restrictions to modifying request->headers_in.headers in NGX_HTTP_PREACCESS_PHASE?
> 
> The module is intended to support distributed tracing in a pluggable way.
> 
> The key/values of the headers added are generated from the module.
> They're used to support cross process tracing
> (http://opentracing.io/documentation/pages/api/cross-process-tracing.html)
> so that the performance information recorded by this module can be linked to the performance information reported by any other code that processes the request.
> 
> Since the specific headers values used to propagate the tracing context across processes vary by tracing system (for example zipkin uses B3 headers https://github.com/openzipkin/b3-propagation), jaeger uses headers like (uber-trace-id, uberctx-*, etc), I'd rather not have any of those details be exposed to the nginx configuration.
> 
> Is there any way any way an arbitrary number of headers can be added without requiring the configuration writer to know anything about them?
> 

A bit hacky, but what you can do is copy the function 'ngx_conf_handler' to your module (you can't use the existing one
since it's static...), then add a command handler -

static char *
ngx_http_my_proxy_set_header(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
	ngx_array_t* old_args;
	ngx_array_t args_arr;
	ngx_str_t args[3] = {
		ngx_string("proxy_set_header"),
		ngx_string("header-name"),
		ngx_string("header-value"),
	};

	args_arr.elts = &args;
	args_arr.nelts = sizeof(args) / sizeof(args[0]);

	old_args = cf->args;
	cf->args = &args_arr;

	if (ngx_conf_handler(cf, 0) != NGX_OK)
	{
		return NGX_CONF_ERROR;
	}

	cf->args = old_args;

	return NGX_CONF_OK;
}

And add this to the commands array of your module -
	{ ngx_string("my_proxy_set_header"),
	NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_NOARGS,
	ngx_http_my_proxy_set_header,
	NGX_HTTP_LOC_CONF_OFFSET,
	0,
	NULL },

nginx.conf will look like this -
location /something/ {
    proxy_pass http://my_upstream;
    my_proxy_set_header;
}

In this specific example, the my_proxy_set_header directive will essentially translate to 'proxy_set_header header-name header-value',
and you can repeat this multiple times to add multiple headers.

Eran



More information about the nginx-devel mailing list