Handler modules : Content Handler vs Content Phase Handler
mdounin at mdounin.ru
Fri Sep 4 18:47:12 UTC 2015
On Fri, Sep 04, 2015 at 12:56:24PM -0400, goldfluss wrote:
> Hello !
> I'm developping some handler modules for nginx and I'm wondering why they
> are two kinds of handlers :
> - Content Phase Handler
> - Content Handler
> I read this interesting blog : http://www.nginxguts.com/2011/01/phases/
> As far as I understood, the content handler could only be called once in a
> location configuration and the content phase handler is called everytime.
> Is there a reason to prefer one type instead of another?
> My last question is if there is a solution to develop content phase handler
> modules without calling them each time, but only if they are activated in
> the nginx.conf file?
Content handlers are unconditionally called for a location, and
they override any default content phase handlers. Hence content
handlers are used by such modules as proxy, fastcgi, memcached,
empty_gif and so on - when all (or almost all) requests in a
location are expected to be handled by a particular module.
In contrast, content phase handlers are called in order, much like
any other phase handlers. There is no way to avoid calling a
content phase handler in a particular configuration. Instead, a
handler should check the configuration itself, and return
NGX_DECLINED if it's not configured to do anything.
That is, you should use content handlers for modules like proxy,
when you want requests to be handled in a particular way in a
given location. And you can use content phase handlers (which are
more expensive compared to content handlers), when you want to
implement something more generic and selective, i.e., to only
handle certain requests, like index or autoindex modules.
More information about the nginx