Read nginx source code_win32

WBOY
Release: 2016-08-08 09:31:57
Original
1130 people have browsed it

I planned to take a good look at the nginx source code over the weekend, but I ended up playing the game for two days.

I haven’t started compiling nginx yet, cygwin hasn’t been installed yet, and the mirror is not good enough. After reading the articles about the nginx series at http://blog.csdn.net/kenbinzhang/article/category/603177, I have a general impression of the nginx project structure.

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;

    ngx_debug_init();

    ngx_strerror_init();

    ngx_get_options(argc, argv);	// 解析程序参数,设置对应的全局变量

    if (ngx_show_version) {
		// 输出nginx版本

        if (ngx_show_help) {
			// 输出程序帮助
        }

        if (ngx_show_configure) {
			// 输出nginx编译配置
        }

        if (!ngx_test_config) {
            return 0;
        }
    }

	/* 计算了一下每种日期格式的字符串长度,并调用ngx_time_update。
	每调用一次ngx_time_update,会更新一个日期字符串cache对应slot里面的日期字符
	更新顺序为0~63 0~63 …… Question:它这个Cache有什么用?*/
    ngx_time_init();	// TODO

#if (NGX_PCRE)
    ngx_regex_init();	// 初始化正则表达式库
#endif

    ngx_pid = ngx_getpid();
	/*初始化日志文件。ngx_prefix是在ngx_get_options函数中解析出来的,
	如果没有制定-p参数,那么它就是NULL,此时,程序采用NGX_PREFIX作为日志存储目录。
	*/
    log = ngx_log_init(ngx_prefix);

    /* STUB */
#if (NGX_OPENSSL)
    ngx_ssl_init(log);
#endif

    ngx_memzero(&init_cycle, sizeof(ngx_cycle_t));
    init_cycle.log = log;
    ngx_cycle = &init_cycle;
    init_cycle.pool = ngx_create_pool(1024, log);
	
	/*argc存到了ngx_argc变量中,argv存到了ngx_os_argv变量中、每个命令行参数
	存到了ngx_argv变量中,ngx_os_environ记录了程序环境变量。*/
    ngx_save_argv(&init_cycle, argc, argv);
	
	/*初始化了init_cycle中关于conf文件的一些配置*/
    ngx_process_options(&init_cycle);
	
	/*获取操作系统信息。这个链接到的是ngx_win32_init.c中的函数。
	这个函数里面调用了WSAStartup来初始化winsock。同时,这个函数里面还通过
	WSAIoctl的SIO_GET_EXTENSION_FUNCTION_POINTER选项获取socket的扩展API*/
    ngx_os_init(log);

    ngx_crc32_table_init();
	// Question: 没看明白
    ngx_add_inherited_sockets(&init_cycle);

    ngx_max_module = 0;
	// 网上说ngx_modules变量是在运行auto/configure脚本的时候生成的
    for (i = 0; ngx_modules[i]; i++) {
        ngx_modules[i]->index = ngx_max_module++;
    }

	/* 这个函数有点长,粗略看了一下,包含初始化init_cycle中一些重要的成员
	配置解析、配置文件解析等*/
    cycle = ngx_init_cycle(&init_cycle);

    if (ngx_test_config) {
        return 0;
    }

    if (ngx_signal) {
        return ngx_signal_process(cycle, ngx_signal);
    }

    ngx_os_status(cycle->log);

    ngx_cycle = 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;
    }

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

    ngx_log_redirect_stderr(cycle);

    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 {
        ngx_master_process_cycle(cycle);
    }

    return 0;
}
Copy after login
is a little sleepy, let’s continue watching another day. .

The above introduces reading nginx source code_win32, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!