What is the order of module execution and how can I change it?

Maxim Dounin mdounin at mdounin.ru
Wed Oct 22 16:38:40 UTC 2014


On Wed, Oct 22, 2014 at 12:26:56PM +0300, Alexander Todorov wrote:

> Hello folks,
> I'm looking into nginx for developing custom modules and I'm missing some
> info related to the order of module execution.
> At http://www.evanmiller.org/nginx-modules-guide.html it says:
> "
> The order of their execution is determined at compile-time. Filters have the
> classic "CHAIN OF RESPONSIBILITY" design pattern: one filter is called, does
> its work, and then calls the next filter, until the final filter is called,
> and Nginx finishes up the response.
> "
> Not a mention about non-filter modules. Then when trying to build nginx from
> source I see a objs/ngx_modules.c file with:
> ngx_module_t *ngx_modules[] = {
>     &ngx_core_module,
>     &ngx_errlog_module,
>     &ngx_conf_module,
>     &ngx_events_module,
>     &ngx_event_core_module,
>     &ngx_epoll_module,
>     &ngx_regex_module,
>     &ngx_http_module,
>     &ngx_http_core_module,
>     &ngx_http_log_module,
> ... skip ...
>     &ngx_http_headers_filter_module,
>     &ngx_http_copy_filter_module,
>     &ngx_http_range_body_filter_module,
>     &ngx_http_not_modified_filter_module,
>     NULL
> };
> This looks like the order of execution of all modules, is this correct ?

Not exactly.  This is a list of all modules.  It also defines 
module registration order (i.e., the order in which various 
init handlers are called).

> If so, where can I place my modules in this list ? Only at the end or at
> arbitrary positions? How do I do that (point me to docs is fine) ?

The order is defined during ./configure.  You may want to take a 
look at auto/modules if you want to understand it better.

> For example I'd like to have a custom logging module which executes after
> ngx_http_log_module saving results to a DB for further analysis and BI
> purposes.

Add your module to the HTTP_MODULES modules list, and then 
register to log phase in your module postconfiguration handler.

> Another example is if I'd want to minify HTML content (a filter module) how
> do I do that before gzip kicks in?

Add your module to the HTTP_AUX_FILTER_MODULES list, and add a 
filter to header/body filter chains in your module 
postconfiguration handler.

Maxim Dounin

More information about the nginx-devel mailing list