Calling custom nginx module after auth_request

318 Views Asked by At

I have a custom nginx module that sets some header to request before proxying request to a server. I also use auth_request for authentication. My location section contains auth_request as well as custom module directive. I want to ensure that the custom header is set only after the auth sub-request and not in the sub-request. Currently I see that the custom module is called first and header is set in the sub request itself.

1

There are 1 best solutions below

0
On

There are two ways to order the modules in nginx.

When adding the module handler, it gets added to a phase.

ngx_http_handler_pt *h = ngx_array_push(&cmcf->phases[NGX_HTTP_CONTENT_PHASE].handlers);
if(h == NULL)
{
    return NGX_ERROR;
}

*h = ngx_dcs_handler;

In this example, we see that the module gets added to the NGX_HTTP_CONTENT_PHASE. I would think that the auth_request gets added in a different earlier phase?

The only other way is to recompile in the correct order. The first module to run is the last on the configure command line.

configure ... --add-module=C --add-module=B --add-module=A

In this case, module A runs first, then B, and finally C.

I'm not too sure whether core modules can as easily be reordered.