About the phase handlers init

Maxim Dounin mdounin at mdounin.ru
Fri Sep 3 19:34:48 UTC 2021


Hello!

On Fri, Sep 03, 2021 at 12:08:32PM +0800, Jinhua Luo wrote:

> In the function ngx_http_init_phase_handlers, I found two places hard
> to understand.
> 
> a) The n variable is as known as the next phase handler index. In the
> switch case NGX_HTTP_ACCESS_PHASE, it invokes n++, which seems to be
> redundant and wrong, While in switch case NGX_HTTP_POST_ACCESS_PHASE,
> it lacks n++. Think about that if the number of modules registers as
> NGX_HTTP_ACCESS_PHASE is 0, then the next index is 1 more than the
> correct number. Of course, the current codes have at least one
> NGX_HTTP_POST_ACCESS_PHASE module: ngx_http_access_module, but it
> contains the codes to jump to the next phase handler, then the jump
> target is bypass NGX_HTTP_POST_ACCESS_PHASE and to
> NGX_HTTP_PRECONTENT_PHASE?

The NGX_HTTP_POST_ACCESS_PHASE is a special phase which is only 
used when there are modules in the NGX_HTTP_ACCESS_PHASE, and only 
contains a single phase handler, ngx_http_core_post_access_phase(). 
The post-access phase does not need to be present at all when there 
are no modules in the access phase.  Further, when access phase 
checker decides to skip the access phase when access is allowed by 
a handler, and needs to switch to the next phase - it does not 
need to switch to post-access phase, but rather to the phase after 
it.  Hence n++ in the access phase case (and not in the 
post-access phase).

> b) Does the handler runs in reverse order of registration order?
> 
> for (j = cmcf->phases[i].handlers.nelts - 1; j >= 0; j--)

Yes.  In particular, this ensures that addon modules are called 
first, before the built-in modules.

-- 
Maxim Dounin
http://mdounin.ru/


More information about the nginx-devel mailing list