Rewrite handling order

Ruslan Ermilov ru at nginx.com
Tue Jan 14 14:57:41 UTC 2014


On Tue, Jan 14, 2014 at 06:54:44PM +0530, Fasih wrote:
> Thanks! Could you please explain why this is done?

Modules register their handlers (at different phases
of request processing) one by one, by adding an element
into the corresponding array of handlers.  The order
in which modules do this is somewhat important.

For example, let's take a look at three standard "index"
modules: autoindex, index, and random_index.  They are
listed in auto/modules in the above mentioned sequence,
and thus register their handlers in this sequence too.
Their handlers are called in a reverse sequence, so it's
either random index, or an index file, or an automatically
generated directory listing, in this order.

There are also 3rd party modules that are added to the
list of modules at the end, and consequently register
their handlers after the standard modules.  Now imagine
you wrote a custom my_index module.  By processing
handlers in a reverse order we give better chance for
the 3rd party module to run.  If we did the opposite,
then nginx would check the "index.html" file existence
(the index module) before even calling your handler.

> On Tue, Jan 14, 2014 at 4:41 PM, Maxim Dounin <mdounin at mdounin.ru> wrote:
> 
> > Hello!
> >
> > On Tue, Jan 14, 2014 at 04:15:32PM +0530, Fasih wrote:
> >
> > > Hi
> > >
> > > I have a custom plugin that handles rewrite (NGX_HTTP_REWRITE_PHASE).
> > There
> > > is another plugin compiled before my plugin that also handles rewrite
> > > (HttpLuaModule). I was expecting to see that my module would rewrite
> > after
> > > lua is done, however that is not the case. Some debugging showed that
> > > whereas my module pushed into the
> > > cmcf->phases[NGX_HTTP_REWRITE_PHASE].handlers after lua, the
> > > cmcf.phase_engine.handlers had lua *after* my module. The culprit seems
> > to
> > > be the following:
> > >
> > > static ngx_int_t
> > > ngx_http_init_phase_handlers(ngx_conf_t *cf, ngx_http_core_main_conf_t
> > > *cmcf)
> > > {
> > > ..
> > >         ph = cmcf->phase_engine.handlers;
> > > ..
> > >         n += cmcf->phases[i].handlers.nelts;
> > >
> > >         for (j = cmcf->phases[i].handlers.nelts - 1; j >=0; j--) {
> > >             ph->checker = checker;
> > >             ph->handler = h[j];
> > >             ph->next = n;
> > >             ph++;
> > >         }
> > > }
> > >
> > > The order is inverted here (h[j] before h[j-1]). Is this intentional or a
> > > bug?
> >
> > It's intentional.
> >
> > --
> > Maxim Dounin
> > http://nginx.org/
> >
> > _______________________________________________
> > nginx-devel mailing list
> > nginx-devel at nginx.org
> > http://mailman.nginx.org/mailman/listinfo/nginx-devel
> >


-- 
Ruslan Ermilov



More information about the nginx-devel mailing list