Home  >  Article  >  Backend Development  >  [nginx source code analysis] Main function analysis

[nginx source code analysis] Main function analysis

WBOY
WBOYOriginal
2016-08-08 09:24:52894browse

ngx_strerror_init

The main function is to first save the error information in the ngx_sys_errlist array and make a mapping array of errno->errmsg. When an error occurs in the system, errmsg can be found directly through errno, reducing api calls.

ngx_get_options(argc, argv)

This function mainly sets some flag variables based on user input and saves the parameters entered by the user.


Param		flag
?/h			ngx_show_version=1/ngx_show_help=1
v			ngx_show_version=1
V			ngx_show_version=1/ngx_show_configure=1
t			ngx_test_config=1
q			ngx_quiet_mode=1
p			ngx_prefix=argv[index]支持-p/home/work/nginx or –p /home/work/nginx
c			ngx_conf_file=p支持方式同上
g			ngx_conf_param=p	支持方式同上
s			ngx_signal=p/ngx_process=NGX_PROCESS_SIGNALLER(stop、quit、reopen、reload)
xngx_time_init- & gt; ngx_time_update

to update the following time

Rreee

ngx_regex_init ()

Mainly set two PCRE global allocation and recycling functions

ngx_pid = getpid (); //Get the pid of the main process

ngx_log_init(ngx_prefix)

variable									format
ngx_cached_time = tp;						
ngx_cached_http_time.data = p0;				“Mon, 28 Sep 1970 06:00:00 GMT”
ngx_cached_err_log_time.data = p1;			“1970/09/28 12:00:00”
ngx_cached_http_log_time.data = p2;			“28/Sep/1970:12:00:00 +0600”
ngx_cached_http_log_iso8601.data = p3		        “1970-09-28T12:00:00+06:00”

The final return value and the ngx_log constructed by the function

#if (NGX_WIN32)
     if (name[1] != ':') {
#else
     if (name[0] != '/') {
#endif

         if (prefix) {
             plen = ngx_strlen(prefix);//如果用户输入了-p prefix

         } else {
#ifdef NGX_PREFIX
             prefix = (u_char *) NGX_PREFIX;//用户没输入,使用configure路径
             plen = ngx_strlen(prefix);
#else
             plen = 0;
#endif
         }

         if (plen) {
             name = malloc(plen + nlen + 2);
             if (name == NULL) {
                 return NULL;
             }

             p = ngx_cpymem(name, prefix, plen);//copy prefix

             if (!ngx_path_separator(*(p - 1))) {
                 *p++ = '/';
             }

             ngx_cpystrn(p, (u_char *) NGX_ERROR_LOG_PATH, nlen + 1);//copy path

             p = name;
         }
     }

init_cycle.pool = ngx_create_pool(1024, log);//Allocate a memory pool node, specific memory Pool, you can

See the memory pool analysis document for details

ngx_save_argv

The function saves the parameters entered by the user in the ngx_argv array

ngx_process_options

This function mainly processes the parameters entered by the user, configuration files, test configuration files,

conf_file =/home/work/hunter/nginx/nginx_gdb/conf/nginx.conf

conf_prefix =/home/work/hunter/nginx/nginx_gdb/conf

prefix =/home/work/hunter/nginx/nginx_gdb

ngx_crc32_table_init()//Initialize the crc table

//Set the module index of each module

ngx_log
{
	ngx_log.file = &ngx_log_file;
	{
		ngx_fd_t		fd;//打开的错误日志
		ngx_str_t		name;//未设置
		u_char		*buffer;//未设置
		u_char		*pos;//未设置
		u_char		*last;//未设置
	}
	log_level = NGX_LOG_NOTICE;
}

Then analyze the configuration parsing

ngx_init_cycl()

The above introduces the [nginx source code analysis] main function analysis, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
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