理解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. mysql_DML_insert

    1.指定字段插入数据 insert into wsb2(stu_name,salary)values ('nan','10000'); insert into wsb2(stu_name,salary ...

  2. 20145102 Java 实验一

    20145102 Java 实验一 Java环境的安装 就像第一周写的一样,在linux下java的安装和配置简直简单的不行不行的,一个命令足以: sudo pacman -S jdk 配置什么的也就 ...

  3. hdu 4421 Bit Magic

    [题意] 这个函数是给A求B的,现在给你B,问你是否能有A的解存在. [2-SAT解法] 对于每个A[i]的每一位运行2-sat算法,只要跑到强连通就可以结束,应为只要判断是否有解,后面拓扑求解就不需 ...

  4. css 雪碧图的制作

    很多网站其实都用了雪碧图,确实方便了制作,以前以为这种小图标,都是一个一个图片呢(笑) 效果图如下: 代码如下: <html> <head lang="en"&g ...

  5. 设置input(radio,checkbox)和lable对齐的问题

    在做页面的时候几次遇到label和前面的小图标无法对齐的情况,后来发现解决方法不过是 label { display:inline-block; vertical-align:top; line-he ...

  6. Cocos2d-x移植到WindowsPhone8移植问题-libNetwork库移植问题

    Cocos2d-x提供了libNetwork库的源代码,而且还提供了libNetwork库的源代码Visual Studio 2012工程文件libNetwork.vcxproj,这样就更方便移植了. ...

  7. 20140215-搭建phonegap开发环境

    1.安装jdk,配置环境变量 a)设置 JAVA_HOME =E:\SetUpFiles\win8.1\Java\jdk1.6.0_45 b)设置PATH=E:\SetUpFiles\win8.1\J ...

  8. (转)RabbitMQ消息队列(四):分发到多Consumer(Publish/Subscribe)

    上篇文章中,我们把每个Message都是deliver到某个Consumer.在这篇文章中,我们将会将同一个Message deliver到多个Consumer中.这个模式也被成为 "pub ...

  9. 6款基于SVG的HTML5CSS3应用和动画

    1.CSS3/SVG质感背景小图标 镂空效果图标按钮 今天我们来分享一款用CSS3和SVG实现的质感背景小图标,鼠标滑过图标时出现镂空的效果,并且有质感背景的描边,效果非常不错. 在线演示 源码下载 ...

  10. 10款很酷的HTML5动画和实用的HTML5应用

    1.HTML5的画布花朵生成器可生成多种样式的花朵 HTML5非常流行,利用HTML5制作动画也非常方便,今天要分享一款利用HTML5 Canvas制作的花朵生成器,我们只需要在Canvas画布上点击 ...