nginx0.1.0之http模块初始化源码分析

2024-03-27 21:48

本文主要是介绍nginx0.1.0之http模块初始化源码分析,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

http模块的初始化类似event模块,初始化的起点在解析到http指令的时候。对应的处理函数是ngx_http_block,因为该函数比较长,所以我们分段解析。第一部分先解析http模块的pre_conf、create_main_conf函数的实现。

static char *ngx_http_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{char                        *rv;ngx_uint_t                   mi, m, s, l, p, a, n;ngx_uint_t                   port_found, addr_found, virtual_names;ngx_conf_t                   pcf;ngx_array_t                  in_ports;ngx_listening_t             *ls;ngx_http_listen_t           *lscf;ngx_http_module_t           *module;ngx_http_handler_pt         *h;ngx_http_conf_ctx_t         *ctx;ngx_http_in_port_t          *in_port, *inport;ngx_http_in_addr_t          *in_addr, *inaddr;ngx_http_server_name_t      *s_name, *name;ngx_http_core_srv_conf_t   **cscfp, *cscf;ngx_http_core_loc_conf_t    *clcf;ngx_http_core_main_conf_t   *cmcf;
#if (WIN32)ngx_iocp_conf_t             *iocpcf;
#endif/* the main http context */ngx_test_null(ctx,ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t)),NGX_CONF_ERROR);// 创建一个ngx_http_conf_ctx_t结构体挂载到cycle的ctx中*(ngx_http_conf_ctx_t **) conf = ctx;/* count the number of the http modules and set up their indices */ngx_http_max_module = 0;// 遍历所有的nginx模块,筛选出http模块for (m = 0; ngx_modules[m]; m++) {if (ngx_modules[m]->type != NGX_HTTP_MODULE) {continue;}// 标记该模块在http中的索引,位置ngx_modules[m]->ctx_index = ngx_http_max_module++;}// 根据http的模块数,分配一个数组,分别由下面三个字段指向/* the main http main_conf, it's the same in the all http contexts */ngx_test_null(ctx->main_conf,ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module),NGX_CONF_ERROR);/* the http null srv_conf, it's used to merge the server{}s' srv_conf's */ngx_test_null(ctx->srv_conf,ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module),NGX_CONF_ERROR);/* the http null loc_conf, it's used to merge the server{}s' loc_conf's */ngx_test_null(ctx->loc_conf,ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module),NGX_CONF_ERROR);/* create the main_conf, srv_conf and loc_conf in all http modules */for (m = 0; ngx_modules[m]; m++) {if (ngx_modules[m]->type != NGX_HTTP_MODULE) {continue;}// http模块的上下文,是ngx_http_module_t结构体module = ngx_modules[m]->ctx;mi = ngx_modules[m]->ctx_index;// 解析http配置前执行的钩子if (module->pre_conf) {if (module->pre_conf(cf) != NGX_OK) {return NGX_CONF_ERROR;}}// 创建一个ngx_http_core_main_conf_t结构体,挂到main_conf数组相应的位置if (module->create_main_conf) {ngx_test_null(ctx->main_conf[mi], module->create_main_conf(cf),NGX_CONF_ERROR);}// 创建一个ngx_http_core_srv_conf_t结构体,挂到srv_conf数组相应的位置if (module->create_srv_conf) {ngx_test_null(ctx->srv_conf[mi], module->create_srv_conf(cf), NGX_CONF_ERROR);}// 创建一个ngx_http_core_loc_conf_s 结构体,挂到loc_conf数组相应的位置if (module->create_loc_conf) {ngx_test_null(ctx->loc_conf[mi], module->create_loc_conf(cf),NGX_CONF_ERROR);}}...
}

下面我们看一下各个http模块的这些钩子函数都做了些什么。

1 pre_conf钩子

log模块
其他模块都是基于log模块的ngx_http_log_fmt_ops变量进行的。

ngx_http_log_op_name_t ngx_http_log_fmt_ops[] = {{ ngx_string("addr"), INET_ADDRSTRLEN - 1, ngx_http_log_addr },{ ngx_string("conn"), NGX_INT32_LEN, ngx_http_log_connection },{ ngx_string("pipe"), 1, ngx_http_log_pipe },{ ngx_string("time"), sizeof("28/Sep/1970:12:00:00") - 1,ngx_http_log_time },{ ngx_string("msec"), TIME_T_LEN + 4, ngx_http_log_msec },{ ngx_string("request"), 0, ngx_http_log_request },{ ngx_string("status"), 3, ngx_http_log_status },{ ngx_string("length"), NGX_OFF_T_LEN, ngx_http_log_length },{ ngx_string("apache_length"), NGX_OFF_T_LEN, ngx_http_log_apache_length },{ ngx_string("i"), NGX_HTTP_LOG_ARG, ngx_http_log_header_in },{ ngx_string("o"), NGX_HTTP_LOG_ARG, ngx_http_log_header_out },{ ngx_null_string, 0, NULL }
};static ngx_int_t ngx_http_log_pre_conf(ngx_conf_t *cf)
{ngx_http_log_op_name_t  *op;for (op = ngx_http_log_fmt_ops; op->name.len; op++) { /* void */ }op->op = NULL;return NGX_OK;
}

gzip模块

static ngx_http_log_op_name_t ngx_http_gzip_log_fmt_ops[] = {{ ngx_string("gzip_ratio"), NGX_INT32_LEN + 3, ngx_http_gzip_log_ratio },{ ngx_null_string, 0, NULL }
};static ngx_int_t ngx_http_gzip_pre_conf(ngx_conf_t *cf)
{ngx_http_log_op_name_t  *op;/*找到第一个没有名字的项,即最后一项,然后把op置空,为了防止下面一个for循环的时候op->op为空,不会导致死循环,因为执行完该函数后,op->op是下一个pre_conf函数执行时,ngx_http_log_fmt_ops数组的最后一个被判断的op->op*/for (op = ngx_http_gzip_log_fmt_ops; op->name.len; op++) { /* void */ }op->op = NULL;op = ngx_http_log_fmt_ops;/*找到最后一个op有值但是没有名字的项,这里其实是一个巧妙的设计,因为op有值但是没有名字的项,op->op指向的是一个ngx_http_log_op_name_t数组,然后op继续指向该数组第一个元素,继续迭代*/for (op = ngx_http_log_fmt_ops; op->op; op++) {if (op->name.len == 0) {op = (ngx_http_log_op_name_t *) op->op;}}/*每个模块中的ngx_http_log_op_name_t数组最后一个元素都是空的,然后在这给op->op赋值,op->op指向的数组的最后一个元素也是空项,以此类推*/op->op = (ngx_http_log_op_pt) ngx_http_gzip_log_fmt_ops;return NGX_OK;
}

userid模块


static ngx_http_log_op_name_t ngx_http_userid_log_fmt_ops[] = {{ ngx_string("uid_got"), 0, ngx_http_userid_log_uid_got },{ ngx_string("uid_set"), 0, ngx_http_userid_log_uid_set },{ ngx_null_string, 0, NULL }
};
static ngx_int_t ngx_http_userid_pre_conf(ngx_conf_t *cf)
{ngx_http_log_op_name_t  *op;for (op = ngx_http_userid_log_fmt_ops; op->name.len; op++) { /* void */ }op->op = NULL;op = ngx_http_log_fmt_ops;for (op = ngx_http_log_fmt_ops; op->op; op++) {if (op->name.len == 0) {op = (ngx_http_log_op_name_t *) op->op;}}op->op = (ngx_http_log_op_pt) ngx_http_userid_log_fmt_ops;return NGX_OK;
}

各模块不一一列举,各模块的pre_conf函数执行完后内存视图是如下。

在这里插入图片描述

2 create_main_conf钩子

1 http_core模块

static void *ngx_http_core_create_main_conf(ngx_conf_t *cf)
{ngx_http_core_main_conf_t *cmcf;ngx_test_null(cmcf,ngx_pcalloc(cf->pool, sizeof(ngx_http_core_main_conf_t)),NGX_CONF_ERROR);ngx_init_array(cmcf->servers, cf->pool,5, sizeof(ngx_http_core_srv_conf_t *),NGX_CONF_ERROR);return cmcf;
}

2 charset_filter模块

static void *ngx_http_charset_create_main_conf(ngx_conf_t *cf)
{ngx_http_charset_main_conf_t  *mcf;if (!(mcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_charset_main_conf_t)))) {return NGX_CONF_ERROR;}ngx_init_array(mcf->charsets, cf->pool, 5, sizeof(ngx_http_charset_t),NGX_CONF_ERROR);ngx_init_array(mcf->tables, cf->pool, 10, sizeof(ngx_http_charset_tables_t),NGX_CONF_ERROR);return mcf;
}

3 log模块

static void *ngx_http_log_create_main_conf(ngx_conf_t *cf)
{ngx_http_log_main_conf_t  *conf;char       *rc;ngx_str_t  *value;if (!(conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_log_main_conf_t)))) {return NGX_CONF_ERROR;}ngx_init_array(conf->formats, cf->pool, 5, sizeof(ngx_http_log_fmt_t),NGX_CONF_ERROR);cf->args->nelts = 0;// 设置参数,在ngx_http_log_set_format里使用if (!(value = ngx_push_array(cf->args))) {return NGX_CONF_ERROR;}if (!(value = ngx_push_array(cf->args))) {return NGX_CONF_ERROR;}value->len = sizeof("combined") - 1;value->data = (u_char *) "combined";if (!(value = ngx_push_array(cf->args))) {return NGX_CONF_ERROR;}*value = ngx_http_combined_fmt;rc = ngx_http_log_set_format(cf, NULL, conf);if (rc != NGX_CONF_OK) {return NULL;}return conf;
}

执行完create_main_conf后的内存视图如下。
在这里插入图片描述
欢迎关注公众号欢迎关注公众号

这篇关于nginx0.1.0之http模块初始化源码分析的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/853494

相关文章

SpringBoot中HTTP连接池的配置与优化

《SpringBoot中HTTP连接池的配置与优化》这篇文章主要为大家详细介绍了SpringBoot中HTTP连接池的配置与优化的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一... 目录一、HTTP连接池的核心价值二、Spring Boot集成方案方案1:Apache HttpCl

基于Go语言实现Base62编码的三种方式以及对比分析

《基于Go语言实现Base62编码的三种方式以及对比分析》Base62编码是一种在字符编码中使用62个字符的编码方式,在计算机科学中,,Go语言是一种静态类型、编译型语言,它由Google开发并开源,... 目录一、标准库现状与解决方案1. 标准库对比表2. 解决方案完整实现代码(含边界处理)二、关键实现细

PostgreSQL 序列(Sequence) 与 Oracle 序列对比差异分析

《PostgreSQL序列(Sequence)与Oracle序列对比差异分析》PostgreSQL和Oracle都提供了序列(Sequence)功能,但在实现细节和使用方式上存在一些重要差异,... 目录PostgreSQL 序列(Sequence) 与 oracle 序列对比一 基本语法对比1.1 创建序

C++类和对象之初始化列表的使用方式

《C++类和对象之初始化列表的使用方式》:本文主要介绍C++类和对象之初始化列表的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录C++初始化列表详解:性能优化与正确实践什么是初始化列表?初始化列表的三大核心作用1. 性能优化:避免不必要的赋值操作2. 强

Spring Boot Controller处理HTTP请求体的方法

《SpringBootController处理HTTP请求体的方法》SpringBoot提供了强大的机制来处理不同Content-Type​的HTTP请求体,这主要依赖于HttpMessageCo... 目录一、核心机制:HttpMessageConverter​二、按Content-Type​处理详解1.

SpringIOC容器Bean初始化和销毁回调方式

《SpringIOC容器Bean初始化和销毁回调方式》:本文主要介绍SpringIOC容器Bean初始化和销毁回调方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录前言1.@Bean指定初始化和销毁方法2.实现接口3.使用jsR250总结前言Spring Bea

Spring实现Bean的初始化和销毁的方式

《Spring实现Bean的初始化和销毁的方式》:本文主要介绍Spring实现Bean的初始化和销毁的方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、Bean的初始化二、Bean的销毁总结在前面的章节当中介绍完毕了ApplicationContext,也就

Python datetime 模块概述及应用场景

《Pythondatetime模块概述及应用场景》Python的datetime模块是标准库中用于处理日期和时间的核心模块,本文给大家介绍Pythondatetime模块概述及应用场景,感兴趣的朋... 目录一、python datetime 模块概述二、datetime 模块核心类解析三、日期时间格式化与

Python如何调用指定路径的模块

《Python如何调用指定路径的模块》要在Python中调用指定路径的模块,可以使用sys.path.append,importlib.util.spec_from_file_location和exe... 目录一、sys.path.append() 方法1. 方法简介2. 使用示例3. 注意事项二、imp

Android实现一键录屏功能(附源码)

《Android实现一键录屏功能(附源码)》在Android5.0及以上版本,系统提供了MediaProjectionAPI,允许应用在用户授权下录制屏幕内容并输出到视频文件,所以本文将基于此实现一个... 目录一、项目介绍二、相关技术与原理三、系统权限与用户授权四、项目架构与流程五、环境配置与依赖六、完整