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

相关文章

怎样通过分析GC日志来定位Java进程的内存问题

《怎样通过分析GC日志来定位Java进程的内存问题》:本文主要介绍怎样通过分析GC日志来定位Java进程的内存问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、GC 日志基础配置1. 启用详细 GC 日志2. 不同收集器的日志格式二、关键指标与分析维度1.

Python中re模块结合正则表达式的实际应用案例

《Python中re模块结合正则表达式的实际应用案例》Python中的re模块是用于处理正则表达式的强大工具,正则表达式是一种用来匹配字符串的模式,它可以在文本中搜索和匹配特定的字符串模式,这篇文章主... 目录前言re模块常用函数一、查看文本中是否包含 A 或 B 字符串二、替换多个关键词为统一格式三、提

MySQL中的表连接原理分析

《MySQL中的表连接原理分析》:本文主要介绍MySQL中的表连接原理分析,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、背景2、环境3、表连接原理【1】驱动表和被驱动表【2】内连接【3】外连接【4编程】嵌套循环连接【5】join buffer4、总结1、背景

springboot如何通过http动态操作xxl-job任务

《springboot如何通过http动态操作xxl-job任务》:本文主要介绍springboot如何通过http动态操作xxl-job任务的问题,具有很好的参考价值,希望对大家有所帮助,如有错... 目录springboot通过http动态操作xxl-job任务一、maven依赖二、配置文件三、xxl-

python中Hash使用场景分析

《python中Hash使用场景分析》Python的hash()函数用于获取对象哈希值,常用于字典和集合,不可变类型可哈希,可变类型不可,常见算法包括除法、乘法、平方取中和随机数哈希,各有优缺点,需根... 目录python中的 Hash除法哈希算法乘法哈希算法平方取中法随机数哈希算法小结在Python中,

Java Stream的distinct去重原理分析

《JavaStream的distinct去重原理分析》Javastream中的distinct方法用于去除流中的重复元素,它返回一个包含过滤后唯一元素的新流,该方法会根据元素的hashcode和eq... 目录一、distinct 的基础用法与核心特性二、distinct 的底层实现原理1. 顺序流中的去重

关于MyISAM和InnoDB对比分析

《关于MyISAM和InnoDB对比分析》:本文主要介绍关于MyISAM和InnoDB对比分析,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录开篇:从交通规则看存储引擎选择理解存储引擎的基本概念技术原理对比1. 事务支持:ACID的守护者2. 锁机制:并发控制的艺

一文深入详解Python的secrets模块

《一文深入详解Python的secrets模块》在构建涉及用户身份认证、权限管理、加密通信等系统时,开发者最不能忽视的一个问题就是“安全性”,Python在3.6版本中引入了专门面向安全用途的secr... 目录引言一、背景与动机:为什么需要 secrets 模块?二、secrets 模块的核心功能1. 基

C++中RAII资源获取即初始化

《C++中RAII资源获取即初始化》RAII通过构造/析构自动管理资源生命周期,确保安全释放,本文就来介绍一下C++中的RAII技术及其应用,具有一定的参考价值,感兴趣的可以了解一下... 目录一、核心原理与机制二、标准库中的RAII实现三、自定义RAII类设计原则四、常见应用场景1. 内存管理2. 文件操

Maven 配置中的 <mirror>绕过 HTTP 阻断机制的方法

《Maven配置中的<mirror>绕过HTTP阻断机制的方法》:本文主要介绍Maven配置中的<mirror>绕过HTTP阻断机制的方法,本文给大家分享问题原因及解决方案,感兴趣的朋友一... 目录一、问题场景:升级 Maven 后构建失败二、解决方案:通过 <mirror> 配置覆盖默认行为1. 配置示