[Nginx source code analysis] configuration parsing 1

Source: Internet
Author: User
: This article mainly introduces [nginx source code analysis] configuration parsing 1. for more information about PHP tutorials, see. The configuration parsing function ngx_init_cycle (& init_cycle) is used for processing.

Ngx_init_cycle (& init_cycle)

Ngx_time_update () // Time update, which is also described in the main function

/** Update the following time by locking and unlocking ngx_cached_time = tp; ngx_cached_http_time.data = p0; latency = p1; ngx_cached_http_log_time.data = p2; latency = p3 ;*/

Log = old_cycle-> log; // error log object

Pool = ngx_create_pool (NGX_CYCLE_POOL_SIZE, log); // allocate a 16 k memory pool. the first node is about 16 k. If a small memory node is added, the maximum size of the node is 4095.

 
Cycle-> pool = pool; cycle-> log = log; cycle-> new_log.log_level = NGX_LOG_ERR; cycle-> conf_prefix; // Set the configuration file dircycle-> prefix; // Set the runtime environment dircycle-> conf_file; // set the absolute path of the configuration file cycle-> conf_param; // Save the parameter cycle-> pathes; // The path array cycle-> open_files; // allocate open file descriptors. The structure is a linked list. each linked list node has n cycle-> shared_memory; // allocate n shared memory nodes, this is a list. each node has n ngx_shm_zone_t structures and points to the node behind the linked list through next. Cycle-> listening; // listening array cycle-> conf_ctx = ngx_pcalloc (pool, ngx_max_module * sizeof (void *));

Assign ngx_max_module context pointers to cycle

First, create the context structure of the core module and save it in the corresponding structure of cycle-> conf_ctx

The core modules are as follows:

NGX_CORE_MODULEindexNgx_core_module0Ngx_errlog_module1Ngx_reg_module6Ngx_events_module3Ngx_http_module7
Core code

     for (i = 0; ngx_modules[i]; i++) {         if (ngx_modules[i]->type != NGX_CORE_MODULE) {             continue;         }         module = ngx_modules[i]->ctx;                                                                                                                                      if (module->create_conf) {             rv = module->create_conf(cycle);             if (rv == NULL) {                 ngx_destroy_pool(pool);                 return NULL;             }             cycle->conf_ctx[ngx_modules[i]->index] = rv;         }     }

Nginx first creates the NGX_CORE_MODULE to pave the way for creating the module context, that is

NGX_CORE_MODULE is the basis of other modules, as follows:

The Ngx_errlog_module, ngx_events_module, and ngx_http_module do not have the create_conf module.

The method is dynamically created based on the user's conf.

     conf.args = ngx_array_create(pool, 10, sizeof(ngx_str_t));      if (conf.args == NULL) {          ngx_destroy_pool(pool);          return NULL;      }       conf.temp_pool = ngx_create_pool(NGX_CYCLE_POOL_SIZE, log);      if (conf.temp_pool == NULL) {          ngx_destroy_pool(pool);          return NULL;      }       conf.ctx = cycle->conf_ctx;      conf.cycle = cycle;      conf.pool = pool;      conf.log = log;      conf.module_type = NGX_CORE_MODULE;      conf.cmd_type = NGX_MAIN_CONF;

Create a parameter for conf to save the user configuration key and value of the configuration file.

Storage pool, conf-> ctx points to cycle-> conf_ctx, saves cycle, pool, and log, and sets the module type and command type,

Start parsing the main configuration file variables.

The process of parsing the configuration file is mainly ngx_conf_parse for indirect recursive calling, mainly divided into main scope, event scope, http scope, server scope, location scope, and then analysis.

During parsing, the configuration file (nginx. conf) is divided into three types:

Enum {parse_file = 0, // Parse the file, such as nginx. conf and include file parse_block, // Parse the block, such as http {} block parse_param // Parse the parameter such as deamon off}

Enter the ngx_conf_parse function, set different values based on whether the file is parsed, the parsing block, and the parsing parameters, and then enter ngx_conf_read_token. this function mainly reads the file and then sets some flag variables, save the user configuration to conf-> args, and parse the configuration file (nginx. conf) when using a buffer memory, mainly read the file, and then parse the buffer, the parsing process is streaming, if the buffer is not parsed, but there is no semicolon or braces, after parsing the file, the file will be moved to the buffer header, and the file will be read and parsed. after each call, the ngx_conf_read_token command will be parsed once. if there is a problem with the configuration file, an error is reported. After parsing a user's settings, save them in the conf-> args array. If cf-> handler has a value, call cf-> handler for callback processing (generally mine. types file). If cf-> handler is empty, call ngx_conf_handler to traverse the cmd of each module, locate the command in case, and call the set callback method of the command, if cmd is a key value variable, after the corresponding module variable is set, ngx_conf_read_token is called. if it is a block key, such as http, event, location, their callback function (cmd-> set) first creates a context structure variable, and then indirectly recursion ngx_conf_parse. of course, different contexts are set.


The above introduces [nginx source code analysis] configuration parsing 1, including some content, and hopes to be helpful to friends who are interested in PHP tutorials.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.