Home >Backend Development >PHP Tutorial >[nginx source code analysis] Configuration analysis 1
The entire configuration analysis is mainly processed by the function ngx_init_cycle(&init_cycle). = ngx_create_pool(NGX_CYCLE_POOL_SIZE,log );//Allocate a 16k memory pool. The first node is about 16k. If a small memory node is added, the maximum node size is 4095. The context structure is stored in the corresponding structure of cycle->conf_ctx
The core module is as follows:
/* * 通过加锁和解锁,来更新如下时间 ngx_cached_time = tp; ngx_cached_http_time.data = p0; ngx_cached_err_log_time.data = p1; ngx_cached_http_log_time.data = p2; ngx_cached_http_log_iso8601.data = p3; */core code
nginx first created NGX_CORE_MODULE to pave the way for creating the subsequent module context, that is,
NGX_CORE_MODULE is the basis for other modules, as follows:
Among them, Ngx_errlog_module, ngx_events_module, and ngx_http_module modules do not have create_confThe method is based on The user's conf is actually created dynamically.
cycle->pool = pool; cycle->log = log; cycle->new_log.log_level = NGX_LOG_ERR; cycle->conf_prefix; //设置配置文件dir cycle->prefix; //设置运行环境dir cycle->conf_file; //设置配置文件绝对路径 cycle->conf_param; //保存参数 cycle->pathes; //路径数组 cycle->open_files; //分配打开的文件描述符,其中这个结构是一 个链表,每个链表节点都有一个n个size的 cycle->shared_memory; //分配n个共享内存节点,这个是一个list, 每个节点都存在n个ngx_shm_zone_t结构,通过next指向链表后面的节点。 cycle->listening; //listening数组 cycle->conf_ctx = ngx_pcalloc(pool, ngx_max_module*sizeof(void*));
Create parameters for conf, which are used to save the user configuration key and value of the configuration file. At the same time, create a
storage pool in temp_pool, conf->ctx points to cycle->conf_ctx, save cycle, pool, log, and set the module Type and command type,
Start parsing the main configuration file variables.
The process of parsing the configuration file is mainly an indirect recursive call by ngx_conf_parse, which is mainly divided into main scope, event scope, http scope, server scope, location scope, and then the analysis is carried out.
During the parsing process, the configuration file (nginx.conf) is divided into three categories:
NGX_CORE_MODULE index Ngx_core_module 0 Ngx_errlog_module 1 Ngx_reg_module 6 Ngx_events_module 3 Ngx_http_module 7Enter the ngx_conf_parse function, set different values according to whether it is parsing files, parsing blocks, and parsing parameters, and then enter ngx_conf_read_token , this function mainly reads the file, then sets some flag variables, saves the user configuration into conf->args, in which a buffer memory is used when parsing the configuration file (nginx.conf), mainly reads the file, and then Parsing the buffer, the parsing process is streaming. If there is still unparsed data behind the buffer, but no semicolons or braces are encountered, the unparsed data will be moved to the head of the buffer, and then the file will continue to be read. For parsing, a command will be parsed every time ngx_conf_read_token is called. If there is a problem with the configuration file, an error will be reported. After parsing a user's settings, they are saved in the conf->args array. If it is found that cf->handler has a value, call cf->handler for callback processing (generally processing the mine.types file). If cf->handler is empty, then call ngx_conf_handler to traverse the cmd of each module. Come to the case, find the command, and call the set callback method of the command, if cmd is a key Value variable setting, after setting the corresponding module variable, call ngx_conf_read_token. If it is a block key, such as http, event, location, their callback function (cmd->set) first creates the context structure variable, and then Indirectly recursive ngx_conf_parse, of course, will set different contexts.
The above introduces [nginx source code analysis] configuration analysis 1, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.