nginx模块定制开发中介入http模块的方法及NGX_HTTP_CONTENT_PHASE阶段的详细介绍

nginx模块定制开发中,大多数都是针对http的模块的定制开发,比如添加一个打印“hello world”的功能、记录http请求内容、对http请求作出一个特殊响应等,此时,需要在nginx源码中的http模块中介入我们的定制代码,我们可以通过必定会被调用的postconfiguration方法,向全局的ngx_http_core_main_conf_t结构的phases[NGX_HTTP_LOG_PHASE+ 1] 添加handler

nginx的HTTP请求分为11个阶段,具体可参考NGINX中HTTP请求的11个处理阶段

此处我们选择在倒数第二个阶段NGX_HTTP_CONTENT_PHASE中介入

static ngx_http_module_t ngx_http_test_module_ctx = {
    NULL,                           /* preconfiguration */
    ngx_http_test_init,            /* postconfiguration */
    NULL,                           /* create main configuration. */
    NULL,                           /* init main configuration. */
    NULL,                           /* create server configuration. */
    NULL,                           /* merge server configuration. */
    ngx_http_test_create_loc_conf, /* create location configuration. */
    NULL                            /* merge location configuration. */
};
 
ngx_module_t ngx_http_test_module = {
    NGX_MODULE_V1, 
    &ngx_http_test_module_ctx, /*module context*/
    ngx_http_test_commands,    /*module directives*/
    NGX_HTTP_MODULE,            /*module type*/
    NULL,                       /*init master*/
    NULL,                       /*init module*/
    NULL,                       /*init process*/
    NULL,                       /*init thread*/
    NULL,                       /*exit thread*/
    NULL,                       /*exit process*/
    NULL,                       /*exit master*/
    NGX_MODULE_V1_PADDING
};

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

    cmcf = (ngx_http_core_main_conf_t*)ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
	//在NGX_HTTP_CONTENT_PHASE中介入处理代码,回调函数ngx_http_test_handler可对http请求做处理
    w = (ngx_http_handler_pt*)ngx_array_push(&cmcf->phases[NGX_HTTP_CONTENT_PHASE].handlers);
    if (w == NULL) 
    {
        return NGX_ERROR;
    }
	//具体实现的回调函数
    *w = ngx_http_test_handler;

	//NGX_HTTP_LOG_PHASE中介入的回调函数用来记录日志
    h = (ngx_http_handler_pt*)ngx_array_push(&cmcf->phases[NGX_HTTP_LOG_PHASE].handlers);
    if (h == NULL)
    {
        return NGX_ERROR;
    }
	//具体实现的回调函数
    *h = ngx_http_test_logger;

    return NGX_OK;
}


NGX_HTTP_CONTENT_PHASE阶段

这是一个核心HTTP阶段,大部分HTTP模块都会在此阶段重新定义Nginx服务器的行为,主要原因如下:

1、ngx_http_phases中的前9个阶段主要专注4件基础工作,rewrite重写URL、找到location配置块、判断请求是否具备访问权限、try_files功能优先读取静态资源文件,这4个工作通常适用于绝大部分请求,因此,许多HTTP模块可以共享这9个阶段中以及完成的功能。

2、NGX_HTTP_CONTENT_PHASE阶段与其他阶段都不同的是,它向HTTP模块提供了两种介入该阶段的方式:第一种与其他10个阶段一样,通过向全局ngx_http_core_main_conf_t结构体的phases数组中添加ngx_http_handler_pt处理方法来实现,而第二种是此阶段独有的,把希望处理请求的ngx_http_handler_pt方法设置到location相关的ngx_http_core_loc_conf_t结构体的handler指针中。

上面说的第一种方式,是通过在必定会被调用的postconfiguration方法向全局的ngx_http_core_main_conf_t结构体的phases[NGX_HTTP_LOG_PHASE+ 1]动态数组添加ngx_http_handler_pt处理方法来达成,此处理方法应用于全部的HTTP请求。

第二种方式通过设置ngx_core_loc_conf_t结构体的handler指针来实现,每一个location都对应一个独立的ngx_core_loc_conf_t结构体,可以选择在ngx_command_t的某个配置项的回调函数中添加处理方法,将当前location块所属的ngx_http_core_loc_conf_t结构体中的handler设置为ngx_http_handler_pt处理方法。这样做的好处是,ngx_http_handler_pt处理方法不再应用于所有的HTTP请求,仅仅当用户请求的URI匹配了location时才会被调用。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值