理解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. Windows装机指南

    开发相关: Anaconda整合了很多python的dependency,方便使用

  2. 代码研磨 Slim v3 (一)--app->get()&route->add()

    index.php代码如下: $app->get('/forbase', function ($request, $response, $args){ Example\Module\Base:: ...

  3. GridView九宫格菜单实现方式

    <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android=&q ...

  4. Part 1 What is AngularJS

    What is AngularJS ? AngularJS is a JavaScirpt framework that helps build web application. AngularJS ...

  5. sql 查询包含字符的数量统计

    );); SELECT @word = 'I do not like to get the news, because there has never been an era when so many ...

  6. asp.net C#生成和解析二维码代码

    类库文件我们在文件最后面下载 [ThoughtWorks.QRCode.dll 就是类库] 使用时需要增加: using ThoughtWorks.QRCode.Codec;using Thought ...

  7. 将ubuntu14.04设置为文本模式启动?

    修改改GRUB 的配置文件(不建议直接改 grub.conf) $sudo vim /etc/default/grub 找到: GRUB_CMDLINE_LINUX_DEFAULT="qui ...

  8. 服务器无法播放flv格式的视频解决办法

    浏览某个网站时播放视频可能会出现下面的情况: 其实原因很简单,因为国内大多都是Win2003的主机 .默认是没有指定输出FLV这种格式的. 虽然FTP里面可以看见,但无法通过http访问,也就无法播放 ...

  9. UI6_UIAlertContrller

    // // ViewController.m // UI6_UIAlertContrller // // Created by zhangxueming on 15/7/7. // Copyright ...

  10. 配置windows下驱动开发环境

    1.下载安装Visual Studio 2008. 2.下载安装WDK 下载地址(点此下载) 建议完全安装,为了便于配置,作者安装到C盘.(C:/WinDDK) 3. ddkwizards(ddkwi ...