nginx代码分析【1】

1. 代码分析准备

a)build脚本

#!/bin/bash                                                                                                                  

/usr/local/nginx/sbin/nginx -s stop
rm -fr /usr/local/nginx/
#./configure --with-debug --add-module=/Users/xuqiang/nginx/nginx/src/modules/
./configure
make -j 8
make install
b) 修改配置文件auto/cc/conf
# add debug mode
ngx_compile_opt="-c -g"

2. 代码分析

main

ngx_debug_init
ngx_strerror_init
ngx_time_init
ngx_regex_init 
ngx_log_init
ngx_log_init
// 因为Nginx支持热切换可执行文件,为了保证在切换前后不丢失所监听的套接字,在切换之前Nginx会把当前的listen fds 写入环境变     量,在切换以后通过读取环境变量获得这些listen fds
ngx_add_inherited_sockets 
// 删除old cycle,生成new cycle,调用ngx_command_t的set函数设定配置文件,调用module的init_module初始化
ngx_init_cycle
ngx_daemon
ngx_create_pidfile
ngx_master_process_cycle
  ngx_start_worker_processes
    创建worker process,进程处理函数为ngx_worker_process_cycle
    ngx_worker_process_cycle内部调用ngx_worker_process_init初始化work process,该函数中处理如下:
     foreach modules { call init_module }
    ngx_start_cache_manager_processes

3. http core module(ngx_http.c)

ngx_http.c中定义了处理http请求的module。command定义如下:

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
};
对应nginx.conf中的如下配置:

http {
    include       mime.types;
    default_type  application/octet-stream;
    //....
}
解析该配置的函数为ngx_http_block,主要逻辑如下:

foreach http modules
{
create_main_conf
preconfiguration
init_main_conf
merge_srv_conf
merge_loc_conf
}
create location trees
ngx_http_init_phases注册各个phrase的handler
我们如果是http module的话,将会被该module初始化。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值