Nginx Configuration File Parsing

Source: Internet
Author: User

There are already many blogs and articles on nginx source code analysis. I have read many of them before and they are all very good at analysis. I don't need to write too much repeated content. I mainly want to find some obscure or details to be discussed during my code analysis and Blog reading, I give my own understanding and opinions, and hope to share and learn with you.

The nginx version used is a nginx-1.0.6, and the code I first looked at was 0.7.62. The new version did a lot of work in terms of functionality and stability.

During the analysis, I try to make it as simple and clear as possible, but it is not very important. You can read the code here. The details of the complex or obscure location are described in detail.

 

First, we start from the configuration file. The following analysis is based on the premise that users are familiar with the nginx configuration file structure, so that they can better understand the code.

 

Note: The ngx_modules structure in the original code directory cannot find its definition and initialization. To See It, You must execute configure, make, an objs folder appears in the original code directory. The three files ngx_auto_config.h, ngx_auto_headers.h, and ngx_modules.c must be included in the source Insight Project, this helps us grasp the entire code structure. Haha, it is interesting that the configure file of nginx is manually written by the author. There are many methods for managing code engineering. If you have time, it is worth learning.

 

1. ngx_cycle_t * ngx_init_cycle (ngx_cycle_t * old_cycle );

Parsing of configuration files is mainly called in the ngx_init_cycle function. In this case, let's talk about the ngx_init_cycle function first.

It requires a parameter type of ngx_cycle_t *, and the returned value is also ngx_cycle_t *. At the same time, we noticed that the parameter name is old_cycle. What is the role of this function? It is obvious that old gets a new value. The ngx_cycle_t structure stores some global configurations and information. The specific role of this function will be reflected in reconfig (re-reading the configuration file). It can be understood that old_cycle is the configuration information currently in use. After some modifications are made to the configuration file, ngx_init_cycle uses some data in old_cycle to set new_cycle. After further configuration parsing, a new
Cycle.

 

2. char * ngx_conf_parse (ngx_conf_t * Cf, ngx_str_t * filename)

When we use sourceinsight to view the call status of this function, we will find that there are many places to call it. In fact, the endpoint is called to ngx_conf_parse in ngx_init_cycle. All subsequent calls can be considered as recursive calls after this. Why is it like this? The reason is that nginx reads configuration information and parses and executes relevant processing. Specifically, it reads one row and executes one row ", the definition of a line here refers to a line ending with a semicolon or "{" and "}". For example, if we resolve it to HTTP {}, we will call the processing for httpblock, when processing, we will encounter the server
{} Will naturally call server block processing... And so on !.

The configuration file is read mainly in the ngx_conf_read_token function. This function reads the configuration information of ngx_conf_buffer (4 kb) every time to the memory Buf and analyzes the Buf. In addition to this function, the return value of this function is mainly used for different processing. For the meaning of its return value, you can read the code carefully. The meaning is clear!

The configuration command can be obtained through the above operations. Strings separated by spaces are saved in a string array (that is, CF-> ARGs). This code is obvious. Then, we will call the ngx_conf_handler function to process the current row configuration. Here we will talk about the working principle of the ngx_conf_handler function in general: "it traverses all the module configurations in the system, finds specific modules, matches specific commands, and then runs". Next, let's expand.

Nginx classifies and manages all modules. Naturally, each module is divided into multiple "sets". commands under the same module are also classified (for example, which modules belong, the location where the configuration is correct, and so on), so each time you call ngx_conf_parse, you will point out "What type is the configuration information I parsed this time", for example:

Conf. module_type = ngx_core_module;
Conf. cmd_type = ngx_main_conf;

That is, the parsed command is used to search for the core module type and find the command with the main conf type in the module.

Before entering the key point, let's take a look at the processing of CF-> handler. What is it? Yes, nginx's general handler ngx_conf_handler is mainly used for the string array of CF-> args. For example, some configurations, such as types and charset_map, are not simple string arrays, the command parameters may be placed in {}, so that the common parsing and processing functions will not apply. By registering CF-> handler, we can perform regular configuration processing on the parameters in {} below.

 

Go to the ngx_conf_handler function and check its working mechanism.

3. Static ngx_int_t

Ngx_conf_handler (ngx_conf_t * Cf, ngx_int_t last)

The last parameter is the returned result of ngx_conf_read_token parsing. Note that the parameters we need have been saved in CF-> args.

The next steps are as follows:

(1) module matching

The code is clearly displayed. It first searches for specific modules based on the type you specify.

(2) command matching

After a matched module is found, the command Array under the module is traversed to find the matched module. If the string matches, you must check the configuration of the command type. If the string does not match, nginx will report it to you.

(3) match the number of parameters

It mainly checks the command that has strict requirements on the number, and directly processes the command of any number (ngx_conf_any.

For the number of parameters, nginx defines some macros, such as ngx_conf_takex (X represents 1, 2, 3, etc., indicating that one, two, and three parameters can be configured ..), There are also 1 more, 2 more, And the meaning is very clear. Note that the maximum number of commands that can be configured is 8, that is, ngx_conf_max_args. Note that when writing your own module, I have suffered losses in this respect...

(4) Execute Command (that is, set function ).

All right, after the previous check is done, you have to perform the relevant operations to generate the module configuration information.

This involves the type of command. It is very important to understand its later understanding. Let's go!

Ngx_direct_conf, ngx_main_conf, ngx_http_main_conf, ngx_http_srv_conf, ngx_http_loc_conf, etc.

Ngx_direct_conf is generally configured outside the HTTP block, ngx_http_main_conf is directly configured in the HTTP block, ngx_http_srv_conf is configured in the server block, ngx_http_loc_conf is configured in the location, and so on.

 

Let's take a look at the CF-> CTX member. Its type is void *. Here you will probably guess its usefulness. It must be transferred in different conditions!

In ngx_init_cycle, we find that the initial value of CF-> CTX is determined by cycle-> conf_ctx, and cycle-> conf_ctx is a void *** type. (-_-!)

In ngx_init_cycle, there is a key sentence:

Cycle-> conf_ctx = ngx_pcalloc (pool, ngx_max_module * sizeof (void *));

Therefore, ngx_max_module is the total number of all modules in the system. In this sentence, conf_ctx is used to occupy a position (a pointer) for each module, but what exactly does each position point? It is a void *, which can be anything. Generally, it only points to the configuration structure of some modules. However, for some modules, we need to obtain it, we need to use N-layer pointers (such as void ***) to obtain them. This design is for logical layering and module division.

After a circle, we found that the final assignment of the members of these pointer arrays will be completed in step 1 here:

There are three cases:

(1) ngx_direct_conf

Configurations other than {} are generally ngx_core_conf_t. During ngx_init_cycle, The ngx_core_module type modules have been initialized (the module requires the init function ), here, according to the configuration information, the set function will assign values to Members in the configuration structure.

(2) ngx_main_conf

Such configuration includes event, HTTP, and so on. They do not have the init function, so the actual space allocation needs to be completed in the Set function, so there is:

Conf = & (void **) CF-> CTX) [ngx_modules [I]-> Index]); // obtain the pointer address

Rv = cmd-> set (CF, CMD, conf); // the pointer is assigned a value in the function.

The conf parameter in set is a double pointer, which has a subsequent statement in ngx_http_block:

CTX = ngx_pcalloc (CF-> pool, sizeof (ngx_http_conf_ctx_t ));

* (Ngx_http_conf_ctx_t **) conf = CTX;

(3) Others

In fact, the "Others" Here are mainly commands of the server and location types. These commands are mostly concentrated in the relevant HTTP configurations.

Let's first look at the ngx_http_conf_ctx_t structure:

Typedefstruct {

Void ** main_conf;

Void ** srv_conf;

Void ** loc_conf;

} Ngx_http_conf_ctx_t;

One thing to note here is that there are some so-called sub-modules in the HTTP module, and these sub-modules are basically some configurations in the server or location in HTTP. These configurations use the ctx_index in the module and save their configuration structure pointers in srv_conf or loc_conf as arrays. This is why their type is void **.

See the following line of code:

CMD-> conf is the displacement of CF-> CTX members. The processing here is to treat the members at this position as a pointer. In fact, it is a second-level pointer, that is, the array of pointers configured by the sub-module!

Confp = * (void **) (char *) CF-> CTX + cmd-> conf );

Conf = confp [ngx_modules [I]-> ctx_index]; // get the Configuration Structure

Rv = cmd-> set (CF, CMD, conf); // Process

 

The void *** type member mentioned at the beginning can be found in the ngx_events_block function.

(To be continued)

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.