nginx做为后端程序不可缺乏的中间件,凭借其优异的性能和稳定,得到了大量的用户nginx
其它目录主要就是存放nginx功能的扩展了、nginx几乎全部的功能都是经过扩展实现的git
src/core/nginx.c::main()github
在入口程序中,nginx主要是处理了命令行参数,若是没有带参数运行nginx,则启动nginx服务器ngx_master_process_cycle(cycle
算法
核心调用以下后端
给启用的nginx模块进行编号服务器
ngx_max_module = 0;
for (i = 0; ngx_modules[i]; i++) {
ngx_modules[i]->index = ngx_max_module++;
}
复制代码
kill()
ngx_int_t
ngx_os_signal_process(ngx_cycle_t *cycle, char *name, ngx_int_t pid)
{
ngx_signal_t *sig;
for (sig = signals; sig->signo != 0; sig++) {
if (ngx_strcmp(name, sig->name) == 0) {
if (kill(pid, sig->signo) != -1) {
return 0;
}
ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
"kill(%P, %d) failed", pid, sig->signo);
}
}
return 1;
}
复制代码
nginx的模块是经过在nginx运行的各个生命周期调用模块的钩子函数实现的 拿src/event/ngx_event.c:184举例网络
ngx_module_t ngx_event_core_module = {
NGX_MODULE_V1,
&ngx_event_core_module_ctx, /* module context */
ngx_event_core_commands, /* module directives */
NGX_EVENT_MODULE, /* module type */
NULL, /* init master */
ngx_event_module_init, /* init module */
ngx_event_process_init, /* init process */
NULL, /* init thread */
NULL, /* exit thread */
NULL, /* exit process */
NULL, /* exit master */
NGX_MODULE_V1_PADDING
};
复制代码
能够看到nginx为每一个模块都支持了7个钩子函数数据结构
回调函数 | 做用 |
---|---|
init master | master进程建立时调用 |
init module | 初始化模块时调用 |
init process | worker进程初始化时调用 |
init thread | 未使用 |
exit thread | 未使用 |
exit process | worker进程退出时调用 |
exit master | master进程退出时调用 |
nginx的事件驱动在src/event/ngx_event.c::ngx_event_core_init_conf(ngx_cycle_t *cycle, void *conf)函数中选择,核心代码以下数据结构和算法
static char * ngx_event_core_init_conf(ngx_cycle_t *cycle, void *conf) {
ngx_event_conf_t *ecf = conf;
...
#if (NGX_HAVE_EPOLL) && !(NGX_TEST_BUILD_EPOLL)
fd = epoll_create(100);
if (fd != -1) {
close(fd);
module = &ngx_epoll_module;
} else if (ngx_errno != NGX_ENOSYS) {
module = &ngx_epoll_module;
}
#endif
...
ngx_conf_init_uint_value(ecf->use, module->ctx_index);
...
}
复制代码
能够看到nginx默认会优先选择epoll网络事件模型进行驱动,日后才是kqueue、select,而后把最后肯定使用的网络驱动模块的索引存到ecf->use这个指针中,后面就能够经过这个use指针调用nginx使用的网络库了ide
在worker进程初始化时:src/event/ngx_event.c:ngx_event_process_init()
for (m = 0; ngx_modules[m]; m++) {
if (ngx_modules[m]->type != NGX_EVENT_MODULE) {
continue;
}
if (ngx_modules[m]->ctx_index != ecf->use) {
continue;
}
module = ngx_modules[m]->ctx;
if (module->actions.init(cycle, ngx_timer_resolution) != NGX_OK) {
/* fatal */
exit(2);
}
break;
}
复制代码
遍历全部模块,找出目前系统选择的网络库ngx_modules[m]->ctx_index != ecf->use
,而后调用初始化函数进行初始化module->actions.init(cycle, ngx_timer_resolution)
actions结构体在src/event/ngx_event.h:ngx_event_actions_t
typedef struct {
ngx_int_t (*add)(ngx_event_t *ev, ngx_int_t event, ngx_uint_t flags);
ngx_int_t (*del)(ngx_event_t *ev, ngx_int_t event, ngx_uint_t flags);
ngx_int_t (*enable)(ngx_event_t *ev, ngx_int_t event, ngx_uint_t flags);
ngx_int_t (*disable)(ngx_event_t *ev, ngx_int_t event, ngx_uint_t flags);
ngx_int_t (*add_conn)(ngx_connection_t *c);
ngx_int_t (*del_conn)(ngx_connection_t *c, ngx_uint_t flags);
ngx_int_t (*process_changes)(ngx_cycle_t *cycle, ngx_uint_t nowait);
ngx_int_t (*process_events)(ngx_cycle_t *cycle, ngx_msec_t timer,
ngx_uint_t flags);
ngx_int_t (*init)(ngx_cycle_t *cycle, ngx_msec_t timer);
void (*done)(ngx_cycle_t *cycle);
} ngx_event_actions_t;
复制代码
初始化在src/event/modules/ngx_epoll_module.c:ngx_event_module_t ngx_epoll_module_ctx
ngx_event_module_t ngx_epoll_module_ctx = {
&epoll_name,
ngx_epoll_create_conf, /* create configuration */
ngx_epoll_init_conf, /* init configuration */
{
ngx_epoll_add_event, /* add an event */
ngx_epoll_del_event, /* delete an event */
ngx_epoll_add_event, /* enable an event */
ngx_epoll_del_event, /* disable an event */
ngx_epoll_add_connection, /* add an connection */
ngx_epoll_del_connection, /* delete an connection */
NULL, /* process the changes */
ngx_epoll_process_events, /* process the events */
ngx_epoll_init, /* init the events */
ngx_epoll_done, /* done the events */
}
};
复制代码
init函数指针对应调用的就是ngx_epoll_init函数了
static ngx_int_t
ngx_epoll_init(ngx_cycle_t *cycle, ngx_msec_t timer)
...
ep = epoll_create(cycle->connection_n / 2);
...
}
复制代码
能够看到主要是调用epoll_create函数了,其他的网络库调用能够经过ngx_event_actions_t、ngx_epoll_module_ctx找到了
本文基于nginx 1.2.0 写成,源码下载参考:nginx.org/download/