nginx http handler 过程

一直容易忘记的httphandle处理过程

ngx_http_handler 起步

void
ngx_http_core_run_phases(ngx_http_request_t *r) //循环处理函数
{
    ngx_int_t                   rc;
    ngx_http_phase_handler_t   *ph;
    ngx_http_core_main_conf_t  *cmcf;

    cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);

    ph = cmcf->phase_engine.handlers;

    while (ph[r->phase_handler].checker) {

        rc = ph[r->phase_handler].checker(r, &ph[r->phase_handler]);

        if (rc == NGX_OK) {
            return;
        }
    }
}

上面代码涉及到cmcf->phase_engine.handlers ,先看一下ngx_http_phase_handler_t 的结构

struct ngx_http_phase_handler_s {
    ngx_http_phase_handler_pt  checker; //阶段性 处理函数
    ngx_http_handler_pt        handler; //真实处理函数
    ngx_uint_t                 next; //链表结构
};

接下来是模块的处理函数,push到cmcf->phases[各个阶段] 的数组中(以gzip为例子):

static ngx_int_t
ngx_http_gzip_static_init(ngx_conf_t *cf)
{
    ngx_http_handler_pt        *h;
    ngx_http_core_main_conf_t  *cmcf;

    cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);

    h = ngx_array_push(&cmcf->phases[NGX_HTTP_CONTENT_PHASE].handlers);
    if (h == NULL) {
        return NGX_ERROR;
    }

    *h = ngx_http_gzip_static_handler;

    return NGX_OK;
}

接下来就是在每个handler函数中去处理,结束返回ngx_OK,想执行下一个返回ngx_again

if (gzcf->enable == NGX_HTTP_GZIP_STATIC_OFF) { //config 是否配置
        return NGX_DECLINED;
    }

    if (gzcf->enable == NGX_HTTP_GZIP_STATIC_ON) { 
        rc = ngx_http_gzip_ok(r);

    } else {
        /* always */
        rc = NGX_OK;
    }

关于NGX_HTTP_CONTENT_PHASE 阶段还有一个单独的优先级非常高的处理函数

ngx_int_t
ngx_http_core_content_phase(ngx_http_request_t *r,
    ngx_http_phase_handler_t *ph){
    ....    
 if (r->content_handler) {
        r->write_event_handler = ngx_http_request_empty_handler; 
        ngx_http_finalize_request(r, r->content_handler(r));//clcf->handler postconfiguration中初始化的优先级高
        return NGX_OK;
    }
    rc = ph->handler(r); // array 中的处理函数

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值