理解HTTP配置

相关数据结构

先明白Nginx下述数据结构,再理解 HTTP配置的解析与合并过程

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;
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;
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配置的更多相关文章

  1. 【转】Nginx模块开发入门

    转自: http://kb.cnblogs.com/page/98352/ 结论:对Nginx模块开发入门做了一个helloworld的示例,简单易懂.也有一定的深度.值得一看. Nginx模块开发入 ...

  2. Nginx模块开发入门

    前言 Nginx是当前最流行的HTTP Server之一,根据W3Techs的统计,目前世界排名(根据Alexa)前100万的网站中,Nginx的占有率为6.8%.与Apache相比,Nginx在高并 ...

  3. [转] Nginx模块开发入门

    前言 Nginx是当前最流行的HTTP Server之一,根据W3Techs的统计,目前世界排名(根据Alexa)前100万的网站中,Nginx的占有率为6.8%.与Apache相比,Nginx在高并 ...

  4. Nginx模块开发入门(转)

    前言 Nginx是当前最流行的HTTP Server之一,根据W3Techs的统计,目前世界排名(根据Alexa)前100万的网站中,Nginx的占有率为6.8%.与Apache相比,Nginx在高并 ...

  5. Nginx模块开发入门(转)

    前言 Nginx是当前最流行的HTTP Server之一,根据W3Techs的统计,目前世界排名(根据Alexa)前100万的网站中,Nginx的占有率为6.8%.与Apache相比,Nginx在高并 ...

  6. FW: Nginx模块开发入门

    前言 Nginx是当前最流行的HTTP Server之一,根据W3Techs的统计,目前世界排名(根据Alexa)前100万的网站中,Nginx的占有率为6.8%.与Apache相比,Nginx在高并 ...

  7. linux下nginx模块开发入门

    本文模块编写参考http://blog.codinglabs.org/articles/intro-of-nginx-module-development.html 之前讲了nginx的安装,算是对n ...

  8. 解剖Nginx·模块开发篇(1)跑起你的 Hello World 模块!

    1 学习 Nginx 模块开发需要有哪些准备? 需要的预备知识不多,有如下几点: 有过一些 C 语言的编程经历: 知道 Nginx 是干嘛的,并有过编写或改写 Nginx 的配置文件的经历. OK,就 ...

  9. nginx模块开发篇 (阿里著作)

    背景介绍 nginx历史 使用简介 nginx特点介绍 nginx平台初探(100%) 初探nginx架构(100%) nginx基础概念(100%) connection request 基本数据结 ...

随机推荐

  1. 新手留言薄asp.net MVC 学习(适合新手学习)

    以下是发布到IIS后的效果截图: 1)首页展示: 2)登录后台页面展示: 3)后台页面展示: 该项目源代码下载地址:http://files.cnblogs.com/files/f12-liugang ...

  2. oracle PL/SQL(procedure language/SQL)程序设计之函数+过程+包

    匿名PL/SQL块回顾 DECLARE (可选)    定义在PL/SQL块中要使用的对象BEGIN (必须)    执行语句EXCEPTION (可选)    错误处理语句END; (必须)匿名块( ...

  3. Umbraco文档类型定义多个template

    利用这个可以同时写PC端和手机端的网站, 在Template中,建立手机端的模板: 在文档类型中,同时选择两个模板,在浏览的时候在URL后加上Template的名称 + .aspx 就可以浏览到你写的 ...

  4. [转]c# xml.Load() locking file on disk causing errors

    本文转自:http://stackoverflow.com/questions/1812598/c-sharp-xml-load-locking-file-on-disk-causing-errors ...

  5. 用xcode6.3编译早期工程时出现Undefined symbols for architecture x86_64错误的解决办法(转)

    Xcode升级到5.1 新特性之一就是默认让所有App都通过64位编译器编译.原来在Xcode5.0.x的时候默认的Standard architectures只有(arm7,armv7s),到5.1 ...

  6. poj1330

    Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24762   Accept ...

  7. 每天一道LeetCode--58. Length of Last Word

    Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...

  8. jQuery$命名冲突问题解决方法

    也许你在看此文章之前还不知道jquery有一个noConflict()东西了,它就是为了避免与其它js插件碰到相同变量的一个解决方法,利用noConflict()可以把变量存到其它指定的变量中去如,我 ...

  9. ASP.NET发布WebService

    1. 创建一个空的Web应用程序 2. 再添加一个Web服务 3. 在所创建Web服务内,编写一算法 4. 写完可直接运行查看结果 5. 项目->右键,发布此WebService 6.  发布至 ...

  10. 《Cocos2d-x实战 JS卷 Cocos2d-JS开发》上线了

    感谢大家一直以来的支持! 各大商店均开始销售:京东:http://item.jd.com/11659698.html当当:http://product.dangdang.com/23659808.ht ...