nginx源码分析之开篇

nginx源码构思精巧,每每阅读颇有收获,写此文与各码农分享

阅读从main开始,流程非常清晰

/* 系统错误初始化,将构建ngx_sys_errlist */

ngx_strerror_init();

/* 选项处理 */
ngx_get_options(argc, argv);

/* 时间初始化, 当前时间:ngx_current_msec */
ngx_time_init();

/* 日志初始化 */
log = ngx_log_init(ngx_prefix);

/* 选项处理 */
ngx_save_argv(..., argc, argv);
ngx_process_options(...);

/* 操作系统初始化处理 */
ngx_os_init(log);

/* 模块点名, ngx_modules代表所有模块,是个数组 */
ngx_max_module = 0;
for (i = 0; ngx_modules[i]; i++) {
    ngx_modules[i]->index = ngx_max_module++;
}

/*
 * 系统初始化,这里将发生配置文件解析,模块上下文注册钩子调用,模块初始化
 * module     : 模块
 * commands  : 模块指令集,负责解析配置文件的选项,一个指令对应一个配置选项
 * conf            : 模块配置结构体,指令解析后的值就是存储在这个里面,每个模块都有自已的一个conf
 * ctx              : 模块上下文,有四种,core, event, http, mail,有注册钩子功能。比如 create conf, init conf
 */
cycle = ngx_init_cycle(&init_cycle);

/* 创建进程id文件 */
ngx_create_pidfile(&ccf->pid, cycle->log);

/*
 * 进程处理
 * 主进程(master)产生多个工作进程(worker)
 * 这里将做各模块进程初始化,监听,接受,请求处理,还有信号等
 */
ngx_master_process_cycle(cycle) {
    ngx_start_worker_processes(cycle, ccf->worker_processes,
                               NGX_PROCESS_RESPAWN) {
        for ( ... ) {
            ngx_spawn_process(cycle, ngx_worker_process_cycle, NULL,
                          "worker process", type);
        }
    }
}


goto: 继续
/* 生产进程都在这个函数里处理 */
ngx_worker_process_cycle {
    /* 各模块进程注册钩子调用 */
    ngx_worker_process_init();
   
    for ( ;; ) {
        ngx_process_events_and_timers(cycle);
    }
}
   

goto: 继续
ngx_process_events_and_timers(cycle) {
    /*
     * 定时器,用红黑树实现,这里找出某个event(事件)的超时时间
     * 每个事件在红黑树里的key的值为:ngx_currnet_msec + 超时时间(默认60s)
     * timer的值为 -1 (如果没有事件),或 ngx_current_msec - key
     */
    timer = ngx_event_find_timer();

    /* epoll 机制,这里将做 epoll_wait(..., timer); */
    ngx_process_events(cycle, timer, flags);
   
    /* 先处理所有可能超时的事件,如果超时,将event的timedout设为1,并且马上event->handler(ev); */
    ngx_event_expire_timers();

    /* 事件存队列方式,开始遍历,调用 */
    ngx_event_process_posted(cycle, &ngx_posted_events);
}


/*
 * 几个重要的结构体
 * ngx_listening_t       : 监听套接字的结构体,比如地址,端口等
 * ngx_connection_t   : 每个socket将对应一个connection,意为连接,里面存着 fd,read(读事件),
 *                               write(写事件) 等。
 * ngx_event_t    : 事件结构体,有个重要的函数指针handler,fd事件被驱动时,将调用这个函数。
 *                               它有几个重要成员
 *                                     timer_set  : 每个event在epoll_wait前,要先进入定时器红黑树,这个标记就是
 *                                                       标记是否在定时器里, 超时处理用的.
 *                                     active       : 当ngx_add_event里(添加或更新事件) 进入epoll时,会置为1.
 *                                     ready       : 进入事件队列里,将置为1,只有为1,它对应的socket fd才可以读
 *                                     timedout   : 此事件对应的socket fd将视为超时
 */


/*
 * http处理
 * 当 listen fd 有连接过来时,它将调用函数 ngx_http_init_connection
 * 当 accept fd 有传送东东时,它将调用函数 ngx_http_init_request,所以的处理都将从这函数开始
 */
ngx_http_init_request {
    ngx_http_process_request_line {
        /* 读请求头 */
        ngx_http_read_request_header(...);

        /* 解析请求行 */
        ngx_http_parse_request_line(...);

        /* 处理请求头部信息 */
        ngx_http_process_request_headers(...) {
            for ( ;; ) {
                /* 解析每一行 */
                ngx_http_parse_header_line(...);
            }

            /* 解析之后对所有行的处理 */
            ngx_http_process_request_header(...);

            /* 真正开始处理请求 */
            ngx_http_process_request(r) {
                ngx_http_handler(r) {
                    /* 非常巧妙的设计处理即将开始 */
                    ngx_http_core_run_phases(r);
                }
            }
        }

    }
}


/*
 * 精巧的设计函数:责任链模式
 * http的每个请求可以分为好几个阶段
 *        规则重写(rewrite)
 *        处理配置(不同url有不同的配置)
 *        权限访问处理
 *        核心内容处理(是走fastcgi,还是直接输出或从缓存获取等)
 *        日志处理
 *
 * 每个阶段都可以由好几个模块处理,这些模块组成一个链,
 * 这是设计模式里的一种,责任链模式
 */
 ngx_http_core_run_phases(r) {
    while (ph[r->phase_handler].checker) {

        rc = ph[r->phase_handler].checker(r, &ph[r->phase_handler]);

        if (rc == NGX_OK) {
            return;
        }
    }
 }


 此文全完,阅读原文:http://nglua.com/reads/1.html

你可能感兴趣的:(nginx源码分析)