Article 20:nginx configuration file format and processing process

Source: Internet
Author: User
Welcome reprint, Reprint please indicate the source http://blog.csdn.net/yankai0219/article/details/8286579
The main contents of this article 0. The format of the Nginx configuration file 1. The format of the configuration file 2 determines that some functions are called recursively.  Reason: 3. Configuration Item Details: Second, nginx structure and function 1. The structure that holds the value of the instruction is 2.  The structure that holds the instruction 3. configuration file-related functions third, configuration file resolution process 1. Total Flow 2. Detailed process Ngx_core_module, Ngx_event_module, Ngx_http_module

0. PrefaceThis article mainly describes the format of the configuration file and how Nginx gets the contents of the configuration file. Read a lot of Daniel's article, also read the code about the core HTTP event module for the configuration file processing, now summed up the content as follows: format of a nginx configuration file 1. CompositionThe Nginx configuration file is composed of multiple configuration items, each with a project name and corresponding project values. The configuration items are divided into simple configuration items and complex configuration items. The project name is also called the Directive (Directive), the project value may be a simple string (ending with a semicolon), or it may be a complex structure of a configuration block combined by a simple string and multiple configuration items (ending in curly braces}), so we can summarize the configuration items into two types: simple configuration items and complex configuration items. As shown in the following illustration:
2. The format of the configuration file determines that some functions are called recursively. Reason: From the above specification you can see that there are recursive thoughts here, so in the following configuration parsing code you can see that some functions are called recursively, and that's why. For complex configuration items, the value is composed of a number of simple/complex configuration items, so nginx does not do meticulous processing, the general is to apply for content space, switching parsing state, and then recursive call analytic function; actually converts the user configuration information to the value of the Nginx variable or the handler function for those simple configuration items
3. Configuration Item Detailed Description:Whether they are simple or complex configuration items, their project name and project values are made up of tags (token). The configuration project name is a token, and the configuration item value can make up one, two, multiple token.       Mark (token): This refers to a string of characters that are separated by spaces, quotes, parentheses, such as ' {', line breaks, and so on, in the contents of a configuration file strings. subcode Examples: For example, simple configuration items: daemon off; Its project name daemon is a token, and the item value off is also a token. And simple configuration items: Error_page 404/404.html; Its project value contains two token, 404 and/404.html respectively. For complex configuration items: location/www {index index.html index.htm index.php;} whose project name is location as a token, the project value is a composite structure of token (/WWW) and multiple simple configuration items. And if I add the following configuration to the configuration file:
Lenky on;
Start Nginx, return the error directly, because for the lenky instruction, Nginx does not have the corresponding code to parse it:
[Emerg]: unknown directive "Lenky" In/usr/local/nginx/conf/nginx.conf:2
structure and function in NginxFor Nginx, we need to write our own configuration file nginx.conf according to certain rules. So what is the form of instructions and instructions in the nginx.conf in the Nginx program? Example: Daemon on;        In the Nginx procedure is how to embody it.      1. The structure that holds the value of the instruction has a type of custom configuration structure in the Nginx, and its format has uniform rules. For HTTPIn the module: ngx_http_<module name>_ (main|srv|loc) _conf_tFor EVENTModule: ngx_event_conf_t for COREModule: ngx_core_conf_t. The role of such a custom configuration structure is to store the value of an instruction.                That is, the Nginx holds the value of the instructions in nginx.conf in these custom configuration structures. For example: ngx_core_conf_t's member Variable ngx_flag_t daemon is the value that holds the instruction daemon. 2. Structure for storing instructionsIn the Nginx, there is a kind of unified instruction structure body ngx_commant_t. In ngx_commant_t ngx_core_commands[]={{instruction A}, {instruction B}, Ngx_null_co Mmand} An explanation of the ngx_commant_t of the directive structural body



3. Functions related to configuration filesNgx_conf_parse and Ngx_conf_handler can refer to Http://lenky.info/2011/09/09/nginx%E9%85%8D%E7%BD%AE%E4%BF%A1%E6%81%AF%E7%9A %84%e8%a7%a3%e6%9e%90%e6%b5%81%e7%a8%8b-3/
third, the process of configuration file parsing 1. Total Flow:Whether for core module or HTTP or EVENT MODULE, the main process is as follows: 1, a void **ctx array is stored by ngx_modules[i]->ctx->create_conf obtained ngx_ XXX_CONF_T structure 2. Structure ngx_conf_t conf.           Fill in several member variables inside. void *ctx is a 1 ctx array, that is, Eg:conf.ctx = ctx ngx_uint_t module_type is filled in as core or HTTP or EVENT. Eg:conf.module_type = Ngx_core_module ngx_uint_t Cmd_type fill out as main or SERVER or LOCATION.eg:conf.cmd_type = Ngx_m         Ain_conf 3. Call the Ngx_conf_parse (ngx_conf_t CONF, XXX) function to parse.         In the Ngx_conf_handler function, void * conf obtains the ngx_xxx_conf_t structure of the module in which a particular token is located by means of an array of CTX, that is, the location of the struct to which a particular token is located. Then point to the Cmd->set function so that the token value can be stored in the structure of a particular token that the Conf points to.        This completes the process of token the value from the configuration file to the program-specific variable. As described in the overall process, the detailed process is also followed by an analysis of Ngx_core_module, Ngx_event_module, and Ngx_http_module.


2. Detailed Process
For Ngx_core_module in the Ngx_init_cycle function


Call Ngx_conf_handler (ngx_conf_t *cf, ngx_int_t last) function in the Ngx_conf_parse function

For daemon



for Ngx_event_module in the Ngx_events_block


for Ngx_http_module
StaticChar*Ngx_http_block(ngx_conf_t *cf, ngx_command_t *cmd,void*conf) {Char*RV;      ngx_uint_t mi, m, s;      ngx_conf_t PCF;      ngx_http_module_t *module;      ngx_http_conf_ctx_t *ctx;      ngx_http_core_loc_conf_t *CLCF;      ngx_http_core_srv_conf_t **CSCFP; ngx_http_core_main_conf_t *cmcf;
/* The main HTTP context *//*1) typedef struct {void **main_conf;     void **srv_conf; void **loc_conf; } ngx_http_conf_ctx_t;  Here the main_conf srv_conf loc_conf are stored as an array of ngx_xxx_conf_t for the HTTP main server location configuration item. * */CTX = Ngx_pcalloc (cf-> pool,sizeof(ngx_http_conf_ctx_t));if(CTX = NULL) { returnNgx_conf_error; }
* (ngx_http_conf_ctx_t * *) conf = CTX;

/* Count the number of the HTTP modules and set up their indices * *
Ngx_http_max_module = 0; for(m = 0; ngx_modules[m]; m++) {if(ngx_modules[m]-> type!= ngx_http_module) {Continue; }
ngx_modules[m]-> ctx_index = ngx_http_max_module++; }

/* The HTTP main_conf context, it's the same in the all HTTP contexts * *

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.