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

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

> -----Original Message-----
> From: nginx-devel [mailto:nginx-devel-bounces at] On Behalf Of Ryan Burn
> Sent: Friday, March 30, 2018 5:30 PM
> To: nginx-devel at; robert at
> 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
> (
> 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, 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] = {

	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"),
	NULL },

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

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.


More information about the nginx-devel mailing list