Report this

What is the reason for this report?

NGINX: How to register phase handler in the request processing flow after a specific standard NGINX module callback?

Posted on February 17, 2021

Hi, as we know NGINX HTTP request passes through a sequence of phases like NGX_HTTP_POST_READ_PHASE and so on and many standard nginx modules register their phase handlers as a way to get called at a specific stage of request processing.

Now, we can build our own module and hook at any phase by doing something like: h = ngx_array_push (& cmcf-> phases[NGX_HTTP_CONTENT_PHASE].handlers);

  • h = ngx_http_hello_handler;
    where cmcf-> phases [NGX_HTTP_CONTENT_PHASE].handlers is an array of handlers for the content phase. This registers our handler in the end of the array.

As multiple modules can register their handlers at a single phase, I am developing a module which needs to register its handler in a specific phase and also after a specific standard nginx module call-back in the array i.e. if ngx_http_realip_module registers its handler at NGX_HTTP_POST_READ_PHASE, i want my handler to hook after this particular module in the sequence.

What would be the best approach to proceed with the same?



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Heya,

Just came across this answer and decided to write some general guidelines for anyone who comes across this in the future despite the old question.

It’s indeed possible to register handlers in specific phases of the HTTP request handling. However, controlling the exact ordering of handlers in a specific phase might be rather tricky, as it often depends on the order of module initializations.

Nginx does not provide a built-in facility for ordering handlers within a phase. Handlers are generally called in the order they were registered. And that order can depend on the order in which modules are listed during Nginx compilation.

You can experiment with adjusting the order of module initializations, but please note it could lead to unintended consequences.

If it’s crucial to have control over the exact order of handler calls, you may need to consider another design approach. A potential method could include combining the functionality of your custom module and ngx_http_realip_module into a single module if it suits your specific needs.

For more in-depth technical details, you may want to directly refer to the Nginx Development Guide.

Hope that this helps!

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.