Only fire a handler once

Yichun Zhang (agentzh) agentzh at
Wed Sep 4 21:38:34 UTC 2013


On Fri, Aug 16, 2013 at 10:28 PM, Aaron Bedra wrote:
> I'm looking for a way to make sure a handler only fires once.

If your handler is possible to run multiple times for the same request
(like the post_subrequest handlers) and you want to avoid that, you
can just use a module ctx field to serve as a flag for that. For

    ctx = ngx_http_get_module_ctx(r, ngx_http_foo_module);
    if (ctx == NULL) {
        /* create ctx here and ctx->already_run should be initialized to 0 */

    if (ctx->already_run) {
        return NGX_DONE;
    /* first time */
    ctx->already_run = 1;

    /* process normally */


More information about the nginx-devel mailing list