今天用了一天的时间看nginx的启动流程,流程还是非常复杂。基本的函数调用有十几个之多。通过看源代码和上网查资料,弄懂了一些函数。有些函数还在学习中,有些函数还待日后学习,这里记录一下今天所学。加油!

http://blog.csdn.net/xiaoliangsky/article/details/39856803

1nginx.c

启动的程序主要在src/core/nginx.c中。和普通函数一样,main函数是其入口函数:以下我们看看main函数的源码:

int ngx_cdecl
main(int argc, char *const *argv)
{
ngx_int_t i;
ngx_log_t *log;
ngx_cycle_t *cycle, init_cycle;
ngx_core_conf_t *ccf; #if (NGX_FREEBSD)
ngx_debug_init();
#endif
//该函数的定义在文件src/os/unix/ngx_errno.c,初始化错误编码
if (ngx_strerror_init() != NGX_OK) {
return 1;
} if (ngx_get_options(argc, argv) != NGX_OK) {//解析Nginx的启动參数
return 1;
} if (ngx_show_version) {
ngx_write_stderr("nginx version: " NGINX_VER NGX_LINEFEED); if (ngx_show_help) {//启动配置參数
ngx_write_stderr(
"Usage: nginx [-? hvVtq] [-s signal] [-c filename] "
"[-p prefix] [-g directives]" NGX_LINEFEED
NGX_LINEFEED
"Options:" NGX_LINEFEED
" -?,-h : this help" NGX_LINEFEED
" -v : show version and exit" NGX_LINEFEED
" -V : show version and configure options then exit"
NGX_LINEFEED
" -t : test configuration and exit" NGX_LINEFEED
" -q : suppress non-error messages "
"during configuration testing" NGX_LINEFEED
" -s signal : send signal to a master process: "
"stop, quit, reopen, reload" NGX_LINEFEED
#ifdef NGX_PREFIX
" -p prefix : set prefix path (default: "
NGX_PREFIX ")" NGX_LINEFEED
#else
" -p prefix : set prefix path (default: NONE)" NGX_LINEFEED
#endif
" -c filename : set configuration file (default: "
NGX_CONF_PATH ")" NGX_LINEFEED
" -g directives : set global directives out of configuration "
"file" NGX_LINEFEED NGX_LINEFEED
);
} if (ngx_show_configure) {
ngx_write_stderr(
#ifdef NGX_COMPILER
"built by " NGX_COMPILER NGX_LINEFEED
#endif
#if (NGX_SSL)
#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
"TLS SNI support enabled" NGX_LINEFEED
#else
"TLS SNI support disabled" NGX_LINEFEED
#endif
#endif
"configure arguments:" NGX_CONFIGURE NGX_LINEFEED);
} if (!ngx_test_config) {
return 0;
}
} /* TODO */ ngx_max_sockets = -1; ngx_time_init();//(core/ngx_time.h)初始化时间和更新系统时间 #if (NGX_PCRE)
ngx_regex_init();//src/core/ngx_regex.c这个函数暂不介绍
#endif ngx_pid = ngx_getpid();//获得当前进程ID log = ngx_log_init(ngx_prefix);//初始化log日志。log是核心模块。以后在学
if (log == NULL) {
return 1;
} /* STUB */
#if (NGX_OPENSSL)
ngx_ssl_init(log);//src/event/ngx_event_openssl.c,这个函数暂不介绍,event模块
#endif /*
* init_cycle->log is required for signal handlers and
* ngx_process_options()
*/ ngx_memzero(&init_cycle, sizeof(ngx_cycle_t));
init_cycle.log = log;
ngx_cycle = &init_cycle; init_cycle.pool = ngx_create_pool(1024, log);//创建内存池
if (init_cycle.pool == NULL) {
return 1;
} if (ngx_save_argv(&init_cycle, argc, argv) != NGX_OK) {//保存配置參数
return 1;
}
//用old_cycle(init_cycle)先保存一些配置信息,然后这个init_cycle将会作为
//ngx_init_cycle參数中的old_cycle
if (ngx_process_options(&init_cycle) != NGX_OK) {
return 1;
}
//调用ngx_os_init()初始化系统相关变量,如内存页面大小ngx_pagesize,ngx_cacheline_size,
//最大连接数ngx_max_sockets等
if (ngx_os_init(log) != NGX_OK) {
return 1;
} /*
* ngx_crc32_table_init() requires ngx_cacheline_size set in ngx_os_init()
*/
//调用ngx_crc32_table_init()初始化CRC表(兴许的CRC校验通过查表进行,效率高)
if (ngx_crc32_table_init() != NGX_OK) {
return 1;
}
//继续sockets,并把相关信息存储在init_cycle
if (ngx_add_inherited_sockets(&init_cycle) != NGX_OK) {
return 1;
}
//初始化各个模块的index
ngx_max_module = 0;
for (i = 0; ngx_modules[i]; i++) {
ngx_modules[i]->index = ngx_max_module++;
}
//调用ngx_init_cycle,这个函数非常重要,大部分初始化操作都在这个函数里面,后面会具体学习
cycle = ngx_init_cycle(&init_cycle);
if (cycle == NULL) {
if (ngx_test_config) {
ngx_log_stderr(0, "configuration file %s test failed",
init_cycle.conf_file.data);
} return 1;
} if (ngx_test_config) {
if (!ngx_quiet_mode) {
ngx_log_stderr(0, "configuration file %s test is successful",
cycle->conf_file.data);
} return 0;
}
//处理signal信号
if (ngx_signal) {
return ngx_signal_process(cycle, ngx_signal);
} ngx_os_status(cycle->log); ngx_cycle = cycle;
//ccf的创建creat_conf和初始化init_conf是在ngx_init_cycle函数中调用的
ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module); if (ccf->master && ngx_process == NGX_PROCESS_SINGLE) {
ngx_process = NGX_PROCESS_MASTER;
} #if !(NGX_WIN32)
//调用ngx_init_signals()初始化信号;主要完毕信号处理程序的注冊
//该函数定义在sorc/os/unix/ngx_process.c
if (ngx_init_signals(cycle->log) != NGX_OK) {
return 1;
}
//若无继承sockets,且设置了守护进程标识,则调用ngx_daemon()创建守护进程
if (!ngx_inherited && ccf->daemon) {
if (ngx_daemon(cycle->log) != NGX_OK) {
return 1;
} ngx_daemonized = 1;
} if (ngx_inherited) {
ngx_daemonized = 1;
} #endif
//调用ngx_create_pidfile()创建进程记录文件,记录进程ID
if (ngx_create_pidfile(&ccf->pid, cycle->log) != NGX_OK) {
return 1;
} if (cycle->log->file->fd != ngx_stderr) { if (ngx_set_stderr(cycle->log->file->fd) == NGX_FILE_ERROR) {
ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
ngx_set_stderr_n " failed");
return 1;
}
} if (log->file->fd != ngx_stderr) {
if (ngx_close_file(log->file->fd) == NGX_FILE_ERROR) {
ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
ngx_close_file_n " built-in log failed");
}
} ngx_use_stderr = 0;
//是单个进程
if (ngx_process == NGX_PROCESS_SINGLE) {
ngx_single_process_cycle(cycle); } else {//master进程
ngx_master_process_cycle(cycle);
} return 0;
}

这里总结函数调用例如以下:

1 ngx_strerror_init()。该函数的定义在文件src/os/unix/ngx_errno.c中。该函数主要初始化系统中

错误编号相应的含义。这样初始化中进行相应的优点是,当出现错误。不用再去调用strerror()函数来获

取错误原因,而直接能够依据错误编号找到相应的错误原因,能够提高执行时的执行效率;

2 ngx_get_options() 解析Nginx的启动參数;

3 ngx_time_init() 该函数的定义在文件src/core/ngx_time.c中。

ngx_cached_err_log_time。

ngx_cached_http_time,ngx_cached_http_log_time,ngx_cached_http_log_iso8601均为

ngx_str_t类型的,用于记录错误日志时间,http缓存时间。http缓存log时间以及iso8061时间,初始化过程中

,先计算该时间表示的字符串的长度,这样能够省却在用到的时候再进行计算。

ngx_cached_time是nginx时间类

型的数据结构,他是volatile类型的,即防止编译器优化,每次都要从内存中读取,而不是用缓存值。这个函数

调用了ngx_time_update()该函数的定义在文件src/core/ngx_time.c中。函数用于更新系统时间;

4 ngx_regex_init() src/core/ngx_regex.c,假设启用了PCRE功能,则进行正則表達式的初始化工作。

Nginx中的pcre主要

是用来支持URL Rewrite的,URL Rewrite主要是为了满足代理模式下,对请求訪问的URL地址进行rewrite操作,来实

现定向訪问。这个函数如今不多介绍。以后学http模块在介绍;

5 ngx_log_init() src/core/ngx_log.c。初始化log日志,包含创建日志文件,log模块也是核心模块,以后在学习。

6 ngx_ssl_init() src/event/ngx_event_openssl.c,这个函数暂不介绍,event模块;

7 ngx_save_argv() 保存启动參数在ngx_argv数组里面,參数个数保存在ngx_argc里面;

8 ngx_process_options() 用old_cycle先保存一些配置信息:prefix,conf_prefix,conf_file, conf_param;

然后这个old_cycle将会作为ngx_init_cycle參数中的old_cycle;

9 ngx_os_init() 初始化系统相关变量。如内存页面大小ngx_pagesize,ngx_cacheline_size,最大连接数ngx_max_sockets等

10 ngx_crc32_table_init() 调用初始化CRC表,兴许的CRC校验通过查表进行,效率高;

11 ngx_add_inherited_sockets() 继承sockets,并把相关信息存储在init_cycle这个后面会具体介绍;

12 初始化各个模块的index http://blog.csdn.net/xiaoliangsky/article/details/39856803

13 ngx_init_cycle() 调用ngx_init_cycle,这个函数非常重要。大部分初始化操作都在这个函数里面,后面会具体学习

14 假如有信号。就调用ngx_siganl_process处理信号

15 ngx_init_signal 初始化信号。主要完毕信号处理程序的注冊

16 若无继承sockets,且设置了守护进程标识,则调用ngx_daemon()创建守护进程

17 ngx_create_pidfile 调用ngx_create_pidfile()创建进程记录文件,记录进程ID

18 进入进程循环,假设ngx_process=1,就进入ngx_single_process_cycle

                                         ngx_process!=1,就进入ngx_master_process_cycle

    ngx_signale_process_cycle和ngx_master_process_cycle这两个函数后面会重点学习。

參考资料:

http://blog.csdn.net/livelylittlefish/article/details/7243718

nginx学习十一 nginx启动流程的更多相关文章

  1. Android FM模块学习之一 FM启动流程

    最近在学习FM模块,FM是一个值得学习的模块,可以从上层看到底层. 上层就是FM的按扭操作和界面显示,从而调用到FM底层驱动来实现广播收听的功能. FM启动流程:如下图: 先进入FMRadio.jav ...

  2. ASP.NET Core MVC 源码学习:MVC 启动流程详解

    前言 在 上一篇 文章中,我们学习了 ASP.NET Core MVC 的路由模块,那么在本篇文章中,主要是对 ASP.NET Core MVC 启动流程的一个学习. ASP.NET Core 是新一 ...

  3. 【转】Nginx学习---深入浅出Nginx的介绍

    [原文]https://www.toutiao.com/i6595428119933354500/ Nginx是一款轻量级的Web服务器.反向代理服务器,由于它的内存占用少,启动极快,高并发能力强,在 ...

  4. Nginx学习---企业级nginx环境搭建

    1.1. nginx安装环境 1.系统要求 nginx是C语言开发,建议在linux上运行,本教程使用Centos6.5作为安装环境. 1-1 安装 GCC 源码安装nginx需要依赖gcc环境,需要 ...

  5. ThinkPHP5.0源码学习之框架启动流程

    ThinkPHP5框架的启动流程图如下: ThinkPHP5的启动流程按照文件分为三步: 1.请求入口(public/index.php) 2.框架启动(thinkphp/start.php) 3.应 ...

  6. nginx学习(2):启动gzip、虚拟主机、请求转发、负载均衡

    一.启用gzip gzip on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.1; gzip_comp_level 2; ...

  7. Nginx学习之六-nginx核心进程模型

    一.Nginx整体架构 正常执行中的nginx会有多个进程,最基本的有master process(监控进程,也叫做主进程)和woker process(工作进程),还可能有cache相关进程. 一个 ...

  8. Nginx学习之四-Nginx进程同步方式-自旋锁(spinlock)

    自旋锁简介 Nginx框架使用了三种消息传递方式:共享内存.套接字.信号. Nginx主要使用了三种同步方式:原子操作.信号量.文件锁. 基于原子操作,nginx实现了一个自旋锁.自旋锁是一种非睡眠锁 ...

  9. uboot学习之uboot启动流程简述

    一.uboot启动分为了三个阶段BL0.BL1.BL2:BL0表示上电后运行ROM中固化的一段程序,其中ROM中的程序是厂家写进去的,所以具体功能可能根据厂家芯片而有所不同.功能如下: 初始化系统时钟 ...

随机推荐

  1. shell单引号屏蔽变量方法

    [goforit ~]$ name="玖零後大叔" [goforit~]$ echo $name 玖零後大叔 [goforit ~]$ echo "$name" ...

  2. 20180929 北京大学 人工智能实践:Tensorflow笔记06

    入戏 需要修改成如下: (完)

  3. Python组织文件 实践:将带有美国风格日期的文件改名为欧洲风格日期

    描述:假设有这样一个任务,你需要将文件名中含有美国风格日期(MM-DD-YYYY)的部分更换为欧洲风格日期(DD-MM-YYYY),并且需要你处理的文件多达上千个 分析:检查当前工作目录的所有文件名, ...

  4. ECNUOJ 2859 表达式的个数

    表达式的个数 Time Limit:5000MS Memory Limit:65536KBTotal Submit:47 Accepted:28 Description  世情薄,人情恶,雨送黄昏花易 ...

  5. [Python] Plotting multiple stocks

    import os import pandas as pd import matplotlib.pyplot as plt def test_run(): start_date='2017-01-01 ...

  6. 使用Java8提供的Duration类制作字幕时间轴调整工具

    网上下载的字幕有时和片源的时间轴不一致.我们能够自己写一个工具来调整,也就是总体向前移动几秒,或者向后移动几秒.Java8中提供的Duration类使得这样的时间计算极其方便.以下就以最简单的srt字 ...

  7. .NET进阶——ORM基础认识

    ORM对象关系映射,对象即实体,关系即关系数据库表.ORM即实现从实体对象关系数据库数据的映射. 本质上就是将数据从一种形式转换到还有一种形式.它详细又有哪些表现呢.咱们一步步学习,一步步分析. OR ...

  8. QThread 爬坑之旅(三种办法解决QObject: Cannot create children for a parent that is in a different thread)

    Cannot create children for a parent that is in a different thread. 在Qt的官方文档,大家知道有两种方式使用QThread. You ...

  9. vue3事件

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. FSM之三--代码风格

    FSM设计之一http://www.cnblogs.com/qiweiwang/archive/2010/11/28/1890244.html Moore型状态机与mealy型状态机相比,由于其状态输 ...