Nginx模块开发-理解HTTP配置
理解HTTP配置
相关数据结构
先明白Nginx下述数据结构,再理解 HTTP配置的解析与合并过程
- ngx_module_t 官方API
typedef struct{
NGX_MODULE_V1;
void *ctx;
ngx_command_t *commands;
ngx_uint_t type;
ngx_int_t (*init_master)(ngx_log_t *log);
ngx_int_t (*init_module)(ngx_cycle_t *cycle);
ngx_int_t (*init_process)(ngx_cycle_t *cycle);
ngx_int_t (*init_thread)(ngx_cycle_t *cycle);
void (*exit_thread)(ngx_cycle_t *cycle);
void (*exit_process)(ngx_cycle_t *cycle);
void (*exit_master)(ngx_cycle_t *cycle);
NGX_MODULE_V1_PADDING;
} ngx_module_t;
- ngx_http_module_t 官方API
typedef struct{
ngx_int_t (*preconfiguration)(ngx_conf_t *cf);
ngx_int_t (*postconfiguration)(ngx_conf_t *cf);
void *(*create_main_conf)(ngx_conf_t *cf);
char *(*init_main_conf)(ngx_conf_t *cf, void *conf);
void *(*create_srv_conf)(ngx_conf_t *cf);
char *(*merge_srv_conf)(ngx_conf_t *cf, void *prev, void *conf);
void *(*create_loc_conf)(ngx_conf_t *cf);
char *(*merge_loc_conf)(ngx_conf_t *cf, void *prev, void *conf);
} ngx_http_module_t;
- ngx_command_t 官方API
typedef struct{
ngx_str_t name;
ngx_uint_t type;
char *(set)(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
ngx_uint_t conf;
ngx_uint_t offset;
void *post;
} ngx_command_t;
HTTP配置的内存布局
typedef struct{
void **main_conf;
void **srv_conf;
void **loc_conf;
} ngx_http_conf_ctx_t;
HTTP配置的解析
主循环=>配置文件解析器=>HTTP框架(ngx_http_module, ngx_http_core_module)
- 主循环调用配置文件解析器解析nginx.conf
- 解析器调用HTTP框架(主要是ngx_http_module, ngx_http_core_module)解析http {}。
- HTTP框架遍历http {}, 创建ngx_http_conf_ctx_t变量,调用modules.ctx.create_main_conf()/create_srv_conf()/create_loc_conf()分配内存,调用modules.ctx.preconfiguration()预先初始化,调用modules.commands.set()解析配置项。
- HTTP框架遍历http {}里的server {}, 创建ngx_http_conf_ctx_t变量, 调用modules.ctx.create_srv_conf()/create_loc_conf()分配内存,调用modules.commands.set()解析配置项。
- HTTP框架遍历server {}里的location {}, 创建ngx_http_conf_ctx_t变量,调用modules.ctx.create_loc_conf()分配内存,调用modules.commands.set()解析配置项。
HTTP配置的合并
- HTTP配置的解析过程为每个http {}, server {}, location {}都创建ngx_http_conf_ctx_t变量, main_conf, srv_conf, loc_conf保存modules.create_main_conf()/create_srv_conf()/create_loc_conf()的结果。同时根据http {}, server {}, location {}的层级关系组织为树状。
- HTTP配置的合并过程实际是分别遍历server {}, location {}层的ngx_http_conf_ctx_t变量及其父级变量,调用modules.ctx.merge_srv_conf()/merge_loc_conf()合并上一级的值。
其中,location {}层调用merge_loc_conf()即可。
理解关键点
- HTTP框架为每个http {}, server {}, location {}都生成一个ngx_http_conf_ctx_t变量。这些变量根据http {}, server {}, location{}的树状关联起来。
- HTTP框架调用modules.ctx.create_main_conf()/create_srv_conf()/create_loc_conf()分配内存。对于http {}还要调用moudles.ctx.preconfiguration()执行预初始化。
- HTTP框架调用modules.commands.set()解析配置项。
- HTTP框架根据树状执行merge操作。就是遍历调用modules.ctx.merge_srv_conf()/merge_loc_conf()操作。
Nginx模块开发-理解HTTP配置的更多相关文章
- 【转】Nginx模块开发入门
转自: http://kb.cnblogs.com/page/98352/ 结论:对Nginx模块开发入门做了一个helloworld的示例,简单易懂.也有一定的深度.值得一看. Nginx模块开发入 ...
- Nginx模块开发入门
前言 Nginx是当前最流行的HTTP Server之一,根据W3Techs的统计,目前世界排名(根据Alexa)前100万的网站中,Nginx的占有率为6.8%.与Apache相比,Nginx在高并 ...
- [转] Nginx模块开发入门
前言 Nginx是当前最流行的HTTP Server之一,根据W3Techs的统计,目前世界排名(根据Alexa)前100万的网站中,Nginx的占有率为6.8%.与Apache相比,Nginx在高并 ...
- Nginx模块开发入门(转)
前言 Nginx是当前最流行的HTTP Server之一,根据W3Techs的统计,目前世界排名(根据Alexa)前100万的网站中,Nginx的占有率为6.8%.与Apache相比,Nginx在高并 ...
- Nginx模块开发入门(转)
前言 Nginx是当前最流行的HTTP Server之一,根据W3Techs的统计,目前世界排名(根据Alexa)前100万的网站中,Nginx的占有率为6.8%.与Apache相比,Nginx在高并 ...
- FW: Nginx模块开发入门
前言 Nginx是当前最流行的HTTP Server之一,根据W3Techs的统计,目前世界排名(根据Alexa)前100万的网站中,Nginx的占有率为6.8%.与Apache相比,Nginx在高并 ...
- linux下nginx模块开发入门
本文模块编写参考http://blog.codinglabs.org/articles/intro-of-nginx-module-development.html 之前讲了nginx的安装,算是对n ...
- 解剖Nginx·模块开发篇(1)跑起你的 Hello World 模块!
1 学习 Nginx 模块开发需要有哪些准备? 需要的预备知识不多,有如下几点: 有过一些 C 语言的编程经历: 知道 Nginx 是干嘛的,并有过编写或改写 Nginx 的配置文件的经历. OK,就 ...
- nginx模块开发篇 (阿里著作)
背景介绍 nginx历史 使用简介 nginx特点介绍 nginx平台初探(100%) 初探nginx架构(100%) nginx基础概念(100%) connection request 基本数据结 ...
随机推荐
- 控制语句(if-else+循环+switch)汇编规则
[1]说说条件码 最常用的的条件码有: CF:进位标志 (无符号溢出) ZF:零标志 SF:符号标志(结果为负数) OF:溢出标志 (补码溢出, 有符号溢出) [2]有两类指令设置条件码而不改变任何其 ...
- amoeba实现MySQL读写分离
amoeba实现MySQL读写分离 准备环境:主机A和主机B作主从配置,IP地址为192.168.131.129和192.168.131.130,主机C作为中间件,也就是作为代理服务器,IP地址为19 ...
- angularJs中筛选功能-angular.filter-1
技术分享:http://www.ncloud.hk/%E6%8A%80%E6%9C%AF%E5%88%86%E4%BA%AB/angular-filter-learn-1/ 以下介绍为自己在使用ang ...
- FastJson的使用方法总结
本文参考:http://www.tuicool.com/articles/zUbQfa 还可参考:https://www.iflym.com/index.php/code/alibaba-fastjs ...
- Matlab的GUI参数传递方式总结
MATLAB GUI传递方式 1.全局变量: 2.作为函数的参数传递: 3.利用控件的userdata数据: 4.为handles结构体添加新字段: 5.setappdata函数为句柄添加数据: 6. ...
- 转:怎么使用github(通俗易懂版)
转: https://www.zhihu.com/question/20070065 作者:珊姗是个小太阳链接:https://www.zhihu.com/question/20070065/ans ...
- 转: ios app架构设计
http://keeganlee.me/post/architecture/20160107 看完这一系列文章后就知道怎么回答这类问题了: App架构设计经验谈:接口的设计 App架构设计经验谈:技术 ...
- MJ刷新控件MJRefreshFooterView上拉之后收不回来的解决办法
修改MJRefreshFooterView.m文件中的这个方法 #pragma mark - 状态相关 #pragma mark 设置状态 - (void)setState:(MJRefreshSta ...
- Linux下用freetds连接mssql中文乱码的问题【参考1】
由于工作原因我们需要通过php访问我们以前的Sql Server 2005数据,所以就有了这篇文章的诞生.废话就少说了,做程序设计的最不喜欢兜圈子了.用简介步骤说明问题,往下看.系统: Linux ...
- C语言知识总结(4)
变量的作用域 C语言根据变量作用域的不同,将变量分为局部变量和全局变量 1.局部变量 1> 定义:在函数内部定义的变量,称为局部变量.形式参数也属于局部变量. 2> 作用域:局部变量只在定 ...