目录

一、http模块的配置

二、HTTP核心模块的数据结构

三、ngx_http_block函数详解

前面几章整理了Nginx的事件模块,这一章开始整理HTTP模块。

HTTP模块的初始化工作,都在src/http/nginx_http.c 中的ngx_http_block函数中进行的。

一、http模块的配置
在看nginx_http_block之前,我们还必须看一下nginx.conf中 HTTP大模块的配置文件,只有理解了HTTP模块的配置,才能更好的理解HTTP模块如何初始化的。

配置如下:

http
{include       mime.types;default_type  application/octet-stream;#charset  gb2312;server_names_hash_bucket_size 128;client_header_buffer_size 32k;large_client_header_buffers 4 32k;client_max_body_size 8m;sendfile on;tcp_nopush     on;keepalive_timeout 60;tcp_nodelay on;fastcgi_connect_timeout 300;fastcgi_send_timeout 300;fastcgi_read_timeout 300;fastcgi_buffer_size 64k;fastcgi_buffers 4 64k;fastcgi_busy_buffers_size 128k;fastcgi_temp_file_write_size 128k;gzip on;gzip_min_length  1k;gzip_buffers     4 16k;gzip_http_version 1.0;gzip_comp_level 2;gzip_types       text/plain application/x-javascript text/css application/xml;gzip_vary on;#limit_zone  crawler  $binary_remote_addr  10m;server{listen       80;#server_name  blog.s135.com;index index.html index.htm index.php;root   /home/wwwroot/;#limit_conn   crawler  20;    location ~ .*\.(php|php5)?${      #fastcgi_pass  unix:/tmp/php-cgi.sock;fastcgi_pass  127.0.0.1:9000;fastcgi_index index.php;include fcgi.conf;}location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)${expires      30d;}location ~ .*\.(js|css)?${expires      1h;}    log_format  access  '$remote_addr - $remote_user [$time_local] "$request" ''$status $body_bytes_sent "$http_referer" ''"$http_user_agent" $http_x_forwarded_for';access_log  /usr/local/nginx-1.4.7/logs/access.log  access;}server{listen  80;server_name  status.blog.s135.com;location / {stub_status on;access_log   off;}}

从上面的配置文件我们可以看到,HTTP的配置主要分为4层:

最外层的http{} 模块。模块类型:NGX_CORE_MODULE (最外层的核心模块)
http核心模块中的main配置:http{include mine type; }。模块类型:NGX_HTTP_MODULE (全局的HTTP模块的配置信息)
http核心模块中的server配置:server{}。模块类型:NGX_HTTP_MODULE (主要是配置Server的信息)
http核心模块中的location本地信息配置:location{}。模块类型:NGX_HTTP_MODULE (主要一个server对应的本地资源信息:静态资源、反向代理端口地址、各种语言容器端口等)
最外层的http模块,类型NGX_CORE_MODULE,属于核心模块,核心模块在最开始配置文件初始化的时候,就会调用指令的命令集。所以在核心模块启动的时候就会调用http的模块配置解析指令函数:ngx_http_block

二、HTTP核心模块的数据结构

/*** HTTP模块命令集* HTTP模块也是一个大模块,最外层为:* http {* ....* }* ngx_http_block:该方法就是回调函数* HTTP核心模块*/
static ngx_command_t  ngx_http_commands[] = {{ ngx_string("http"),NGX_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_NOARGS,ngx_http_block,0,0,NULL },ngx_null_command
};/***HTTP核心模块上下文*/
static ngx_core_module_t  ngx_http_module_ctx = {ngx_string("http"),NULL,NULL
};/*** HTTP核心模块 结构* 模块类型:NGX_CORE_MODULE* 通过调用ngx_http_block方法,解析{}中的HTTP模块配置*/
ngx_module_t  ngx_http_module = {NGX_MODULE_V1,&ngx_http_module_ctx,                  /* module context */ngx_http_commands,                     /* module directives */NGX_CORE_MODULE,                       /* module type */NULL,                                  /* init master */NULL,                                  /* init module */NULL,                                  /* init process */NULL,                                  /* init thread */NULL,                                  /* exit thread */NULL,                                  /* exit process */NULL,                                  /* exit master */NGX_MODULE_V1_PADDING
};

从上面的结构中可以看到http{} 是NGX_CORE_MODULE,在核心模块初始化的时候,会调用ngx_http_commands命令集中的回调函数,逐个解析核心模块的配置信息。

而HTTP模块的总入口就是这个http{}命令集的回调函数:ngx_http_block

如果对配置文件如何解析有遗忘,请回顾《 Nginx源码分析 - 主流程篇 - 解析配置文件 》

三、ngx_http_block函数详解

/***ngx_http_commands 命令集的回调函数*HTTP模块初始化的入口函数**/
static char *
ngx_http_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{char                        *rv;ngx_uint_t                   mi, m, s;ngx_conf_t                   pcf;ngx_http_module_t           *module;ngx_http_conf_ctx_t         *ctx;ngx_http_core_loc_conf_t    *clcf;ngx_http_core_srv_conf_t   **cscfp;ngx_http_core_main_conf_t   *cmcf;if (*(ngx_http_conf_ctx_t **) conf) {return "is duplicate";}/* the main http context *//* 分配一块内存,存放http配置上下文 */ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t));if (ctx == NULL) {return NGX_CONF_ERROR;}*(ngx_http_conf_ctx_t **) conf = ctx;/* count the number of the http modules and set up their indices *//* 计算http模块个数 */ngx_http_max_module = ngx_count_modules(cf->cycle, NGX_HTTP_MODULE);/* the http main_conf context, it is the same in the all http contexts *//*** 最外层的HTTP配置* http{include       mime.types;default_type  application/octet-stream;*/ctx->main_conf = ngx_pcalloc(cf->pool,sizeof(void *) * ngx_http_max_module);if (ctx->main_conf == NULL) {return NGX_CONF_ERROR;}/** the http null srv_conf context, it is used to merge* the server{}s' srv_conf's*//*** server层的配置*   server{listen       80;#server_name  blog.s135.com;index index.html index.htm index.php;root   /home/wwwroot/;*/ctx->srv_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module);if (ctx->srv_conf == NULL) {return NGX_CONF_ERROR;}/** the http null loc_conf context, it is used to merge* the server{}s' loc_conf's*//*** location 层的配置location ~ .*\.(php|php5)?${#fastcgi_pass  unix:/tmp/php-cgi.sock;fastcgi_pass  127.0.0.1:9000;fastcgi_index index.php;include fcgi.conf;}*/ctx->loc_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module);if (ctx->loc_conf == NULL) {return NGX_CONF_ERROR;}/** create the main_conf's, the null srv_conf's, and the null loc_conf's* of the all http modules*//*** 调用:create_main_conf、create_srv_conf、create_loc_conf* 创建配置*/for (m = 0; cf->cycle->modules[m]; m++) {if (cf->cycle->modules[m]->type != NGX_HTTP_MODULE) {continue;}module = cf->cycle->modules[m]->ctx;mi = cf->cycle->modules[m]->ctx_index;if (module->create_main_conf) {ctx->main_conf[mi] = module->create_main_conf(cf);if (ctx->main_conf[mi] == NULL) {return NGX_CONF_ERROR;}}if (module->create_srv_conf) {ctx->srv_conf[mi] = module->create_srv_conf(cf);if (ctx->srv_conf[mi] == NULL) {return NGX_CONF_ERROR;}}if (module->create_loc_conf) {ctx->loc_conf[mi] = module->create_loc_conf(cf);if (ctx->loc_conf[mi] == NULL) {return NGX_CONF_ERROR;}}}pcf = *cf;cf->ctx = ctx;/*** preconfiguration 预先初始化配置信息*/for (m = 0; cf->cycle->modules[m]; m++) {if (cf->cycle->modules[m]->type != NGX_HTTP_MODULE) {continue;}module = cf->cycle->modules[m]->ctx;if (module->preconfiguration) {if (module->preconfiguration(cf) != NGX_OK) {return NGX_CONF_ERROR;}}}/* parse inside the http{} block */cf->module_type = NGX_HTTP_MODULE;cf->cmd_type = NGX_HTTP_MAIN_CONF;rv = ngx_conf_parse(cf, NULL);if (rv != NGX_CONF_OK) {goto failed;}/** init http{} main_conf's, merge the server{}s' srv_conf's* and its location{}s' loc_conf's*//*** 初始化main配置* 合并 server srv_conf* 合并location loc_conf*/cmcf = ctx->main_conf[ngx_http_core_module.ctx_index];cscfp = cmcf->servers.elts;for (m = 0; cf->cycle->modules[m]; m++) {if (cf->cycle->modules[m]->type != NGX_HTTP_MODULE) {continue;}module = cf->cycle->modules[m]->ctx;mi = cf->cycle->modules[m]->ctx_index;/* init http{} main_conf's */if (module->init_main_conf) {rv = module->init_main_conf(cf, ctx->main_conf[mi]);if (rv != NGX_CONF_OK) {goto failed;}}rv = ngx_http_merge_servers(cf, cmcf, module, mi);if (rv != NGX_CONF_OK) {goto failed;}}/* create location trees *//*** 创建 location模块的trees*/for (s = 0; s < cmcf->servers.nelts; s++) {clcf = cscfp[s]->ctx->loc_conf[ngx_http_core_module.ctx_index];if (ngx_http_init_locations(cf, cscfp[s], clcf) != NGX_OK) {return NGX_CONF_ERROR;}if (ngx_http_init_static_location_trees(cf, clcf) != NGX_OK) {return NGX_CONF_ERROR;}}if (ngx_http_init_phases(cf, cmcf) != NGX_OK) {return NGX_CONF_ERROR;}if (ngx_http_init_headers_in_hash(cf, cmcf) != NGX_OK) {return NGX_CONF_ERROR;}for (m = 0; cf->cycle->modules[m]; m++) {if (cf->cycle->modules[m]->type != NGX_HTTP_MODULE) {continue;}module = cf->cycle->modules[m]->ctx;if (module->postconfiguration) {if (module->postconfiguration(cf) != NGX_OK) {return NGX_CONF_ERROR;}}}if (ngx_http_variables_init_vars(cf) != NGX_OK) {return NGX_CONF_ERROR;}/** http{}'s cf->ctx was needed while the configuration merging* and in postconfiguration process*/*cf = pcf;if (ngx_http_init_phase_handlers(cf, cmcf) != NGX_OK) {return NGX_CONF_ERROR;}/* optimize the lists of ports, addresses and server names *//* ngx_http_optimize_servers 初始化listen 端口号 ip地址 服务器等监听信息*/if (ngx_http_optimize_servers(cf, cmcf, cmcf->ports) != NGX_OK) {return NGX_CONF_ERROR;}return NGX_CONF_OK;failed:*cf = pcf;return rv;
}

转载地址:

1. https://initphp.blog.csdn.net/article/details/53704137

Nginx源码分析 - HTTP模块篇 - HTTP模块的初始化(20)相关推荐

  1. nginx源码分析(5)——监听socket初始化

    在nginx源码分析(4)中,看到了nginx的事件模型,但其中没有介绍监听socket的初始化.而对于web server来说,需要通过监听socket来监听客户端的连接等.本篇将会具体介绍这方面的 ...

  2. nginx源码分析(2)——http模块的初始化过程

    前一篇文章介绍了nginx的启动初始化过程,包括了所有模块的初始化过程,比如http模块.事件模块等.这里再详细介绍一下http模块的启动过程,还记得在前一篇文章中提到过ngx_conf_parse函 ...

  3. Nginx 源码分析

    1.工程 ngx_conf_file.c ngx_connection.c ngx_cycle.c ngx_file.h ngx_module.c ngx_open_file_cache.h ngx_ ...

  4. Nginx源码分析:epoll事件处理模块概述

    nginx源码分析 nginx-1.11.1 参考书籍<深入理解nginx模块开发与架构解析> 事件处理模块概述 Nginx的高效请求的处理依赖于事件管理机制,本次默认的场景是Linux操 ...

  5. Nginx 源码分析-- 模块module 解析执行 nginx.conf 配置文件流程分析 一

    搭建nginx服务器时,主要的配置文件 nginx.conf 是部署和维护服务器人员经常要使用到的文件, 里面进行了许多服务器参数的设置.那么nginx 以模块 module为骨架的设计下是如何运用模 ...

  6. nginx源码分析之网络初始化

    nginx作为一个高性能的HTTP服务器,网络的处理是其核心,了解网络的初始化有助于加深对nginx网络处理的了解,本文主要通过nginx的源代码来分析其网络初始化. 从配置文件中读取初始化信息 与网 ...

  7. Nginx源码分析:惊群处理与负载均衡

    nginx源码分析 nginx-1.11.1 参考书籍<深入理解nginx模块开发与架构解析> Nginx的惊群处理与负载均衡概述 当Nginx工作在master/worker模式下时,就 ...

  8. Nginx源码分析:核心数据结构ngx_cycle_t与内存池概述

    nginx源码分析 nginx-1.11.1 参考书籍<深入理解nginx模块开发与架构解析> 核心数据结构与内存池概述 在Nginx中的核心数据结构就是ngx_cycle_t结构,在初始 ...

  9. Nginx源码分析:master/worker工作流程概述

    nginx源码分析 nginx-1.11.1 参考书籍<深入理解nginx模块开发与架构解析> Nginx的master与worker工作模式 在生成环境中的Nginx启动模式基本都是以m ...

  10. Nginx源码分析:启动流程

    nginx源码分析 nginx-1.11.1 参考书籍<深入理解nginx模块开发与架构解析> nginx简介 Nginx的作为服务端软件,表现的主要特点是更快.高扩展.高可靠性.低内存消 ...

最新文章

  1. python构建json_如何使用Python构建JSON API
  2. sm4 的s盒_SM4国密算法Java版
  3. Android 使用自带的HttpClient进行https请求出现403的解决过程记录
  4. Mysql 死锁过程及案例详解之元数据锁MetaData Lock
  5. 如何创建newsstand应用程序
  6. linux如何给vm权限,linux – 如何创建一个每个用户的vm被隔离的环境
  7. [CQOI2014]通配符匹配
  8. 再破记录!2019天猫双11八小时总成交1504.9亿,开场后8分1秒发货量破1亿
  9. python最大迭代次数_python scipy eigs:无论收敛容差如何,在最大迭代次数后返回特征向量...
  10. BigDecimal.divide方法
  11. Thread 线程基础之-线程池ThreadPool一
  12. Aladdin HASP SRM(AES-128)加密狗破解经验分享
  13. tar打包命令的用法
  14. skyeye与uClinux的安装
  15. 百度Sugar数据可视化领域优势地位因何受到挑战?
  16. Microsoft Excel 教程:如何在 Excel 中使用条件格式?
  17. 设有一个线性表E = { e1, e2, … , en - 1, en },设计一个算法,将线性表逆置,即使元素排列次序颠倒过来,成为逆线性表E'={ en , en-1 , … , e2 , e1
  18. 蓝桥杯入门练习题斐波那契数列
  19. switch函数不加break的效果
  20. cpu、内存、磁盘、操作系统的关系

热门文章

  1. L3-001 凑零钱 (30 分)—团体程序设计天梯赛
  2. python3.6_发送邮件
  3. 51nod1355 斐波那契的最小公倍数
  4. Cesium源码剖析---Post Processing之物体描边(Silhouette)
  5. iOS 证书, provision profile作用
  6. 获取table控件的某行某列
  7. PyQt之按钮传递鼠标按下事件点击失效
  8. 测试驱动开发与极限编程思想浅析
  9. jcr一区是什么意思_SCI分区中JCR分区和中科院分区的三点区别
  10. 二、K8s Cluster详细安装步骤