NGINX----源码阅读一(main函数)

摘要:
1、 ngx_调试初始化();初始化调试函数,该函数通常为空。2、 ngx_strerror_init()将系统错误代码+错误消息设置为ngx_ str_ T Array save。3、 ngx_ get_ Options(intargc,char*const*argv)nginx启动功能选项,4,ngx_ show_ version_ info()如果在上一步骤中启用了nginx input v选项,则会显示nginx版本信息

1、ngx_debug_init();

初始化debug函数,一般为空。

2、ngx_strerror_init();

将系统错误码+错误信息,以ngx_str_t数组保存。

3、ngx_get_options(int argc, char *const *argv)

nginx启动函数选项,

4、ngx_show_version_info();

如果上一步启动nginx输入量v选项,则显示nginx版本信息,

5、ngx_time_init();

初始化nginx系统时间。

如下

 1 void
 2 ngx_time_init(void)
 3 {
 4     ngx_cached_err_log_time.len = sizeof("1970/09/28 12:00:00") - 1;
 5     ngx_cached_http_time.len = sizeof("Mon, 28 Sep 1970 06:00:00 GMT") - 1;
 6     ngx_cached_http_log_time.len = sizeof("28/Sep/1970:12:00:00 +0600") - 1;
 7     ngx_cached_http_log_iso8601.len = sizeof("1970-09-28T12:00:00+06:00") - 1;
 8     ngx_cached_syslog_time.len = sizeof("Sep 28 12:00:00") - 1;
 9 
10     ngx_cached_time = &cached_time[0];
11 
12     ngx_time_update();
13 }

6.ngx_pid = ngx_getpid();

获取进程id,在进程文件process.c中,

#define ngx_getpid   getpid

7、ngx_log_init(ngx_prefix);

初始化nginx日志,打开日志文件。ngx_prefix为文件路径,由nginx -p选项确定

nginx日志结构体如下

 1 struct ngx_log_s {
 2     ngx_uint_t           log_level;
 3     ngx_open_file_t     *file;
 4 
 5     ngx_atomic_uint_t    connection;
 6 
 7     time_t               disk_full_time;
 8 
 9     ngx_log_handler_pt   handler;
10     void                *data;
11 
12     ngx_log_writer_pt    writer;
13     void                *wdata;
14 
15     /*
16      * we declare "action" as "char *" because the actions are usually
17      * the static strings and in the "u_char *" case we have to override
18      * their types all the time
19      */
20 
21     char                *action;
22 
23     ngx_log_t           *next;
24 };

 8.ngx_memzero(&init_cycle, sizeof(ngx_cycle_t));初始化全局cycle

1     ngx_memzero(&init_cycle, sizeof(ngx_cycle_t));
2     //初始化init_cycle的log
3     init_cycle.log = log;
4     ngx_cycle = &init_cycle;
5     //给init_cycle创建内存池
6     init_cycle.pool = ngx_create_pool(1024, log);

内存池结构入口

9、ngx_save_argv(&init_cycle, argc, argv) 

保存启动参数,通过alloc函数分配空间。

10、ngx_process_options(&init_cycle) 

将路径及配置路径写入cycle中

1         cycle->conf_prefix.len = len;
2         cycle->conf_prefix.data = p;
3         cycle->prefix.len = len;
4         cycle->prefix.data = p;

11、ngx_os_init(log) ngx_crc32_table_init()不懂

系统初始化

12、ngx_add_inherited_sockets(&init_cycle) 

这个是重启时继承监听端口。不懂

13、ngx_preinit_modules() 

初始化模块,index及name

NGINX----源码阅读一(main函数)第1张NGINX----源码阅读一(main函数)第2张
 1 ngx_int_t
 2 ngx_preinit_modules(void)
 3 {
 4     ngx_uint_t  i;
 5 
 6     for (i = 0; ngx_modules[i]; i++) {
 7         ngx_modules[i]->index = i;
 8         ngx_modules[i]->name = ngx_module_names[i];
 9     }
10 
11     ngx_modules_n = i;
12     ngx_max_module = ngx_modules_n + NGX_MAX_DYNAMIC_MODULES;
13 
14     return NGX_OK;
15 }
View Code

 14、ngx_init_cycle(&init_cycle)

这个函数做的事情比较多,开始先把原来init_cycle里的原始数据进行拷贝

然后创建并解析了核心模块的配置。这个单列出来说吧。

15.ngx_signal_process(cycle, ngx_signal);

如果ngx_signal模式启动nginx的话,nginx的启动准备工作到此就已经结束了

此步,采用非master-workers的启动模式,单进程启动nginx。

16.ngx_os_status(cycle->log);

17.ngx_init_signals(cycle->log);

18.ngx_create_pidfile(&ccf->pid, cycle->log);

19.ngx_master_process_cycle(cycle);

准备工作就绪,这里master就开始真正工作了。单列出来说下。

免责声明:文章转载自《NGINX----源码阅读一(main函数)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇CentOS 多版本 GCC 共存Unicode(UTF-8, UTF-16)令人混淆的概念下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Nginx核心知识100讲学习笔记(陶辉)Nginx架构基础(四)

一、红黑树 1、红黑树 2、红黑树复杂度 3、使用红黑树的模块 1、本地内存做的红黑树 ngx_conf_module ngx_event_timer_rbtree #管理定时器的红黑树 2、管理定时器的红黑树 Ngx_http_file_cache Ngx_http_geo_module Ngx_http_limit_conn_module Ng...

ES9新内容概括

本文主要介绍ES2018 新出的一些特性 1.异步迭代   允许 async/await 与 for-of 一起使用,以串行的方式运行异步操作,达到异步函数的迭代效果。 async function process(array) { for await (let i of array) { doSomething(i); } } 2...

学习Android camera笔记 & 调用流程

参考:http://blog.csdn.net/xingyu19871124/article/details/7750189http://blog.csdn.net/BonderWu/article/details/5814278http://blog.chinaunix.net/uid-2630593-id-3307176.htmlhttp://zhid...

Nginx超时配置

Nginx超时配置1.client_header_timeout语法client_header_timeout time 默认值60s 上下文http server 说明指定等待client发送一个请求头的超时时间(例如:GET/HTTP/1.1).仅当在一次read中,没有收到请求头,才会算超时。如果在超时时间内,client没发送任何东西,nginx返...

Spring bean的初始化及销毁

Spring bean的几个属性:scope、init-method、destroy-method、depends-on等。 Scope 在Spring容器中是指其创建的Bean对象相对于其他Bean对象的请求可见范围。 scope分类:singleton, prototype, request, session, global session。 这里的s...

nginx的平滑升级

一:解释nginx的平滑升级 随着nginx越来越流行,并且nginx的优势也越来越明显,nginx的版本迭代也来时加速模式,1.9.0版本的nginx更新了许多新功能,例如stream四层代理功能,伴随着nginx的广泛应用,版本升级必然越来越快,线上业务不能停,此时nginx的升级就是运维的工作了 Nginx方便地帮助我们实现了平滑升级。其原理简单概括...