In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
< cmcf->Servers.nelts; slots +) {clcf = cscfp [s]-> ctx- > loc_ confs [NGX _ http_core_module.ctx_index]; if (ngx_http_init_locations (cf, cscfp [s], clcf)! = ngx_ok) {return ngx_conf_error;} if (ngx_http_init_static_location_trees (cf, clcf)! = ngx_ok) {return ngx_conf_error }} if (ngx_http_init_phases (cf, cmcf)! = ngx_ok) {return ngx_conf_error;} if (ngx_http_init_headers_in_hash (cf, cmcf)! = ngx_ok) {return ngx_conf_error;} for (m = 0; ngx_ modules [m]; masks +) {if (ngx_ modules [m]-> type! = ngx_http_module) {continue } module = ngx_ modules [m]-> ctx If (module- > postconfiguration) {/ / call the postconfiguration callback function of each http module / ngx_http_log_module ngx_http_log_init / / ngx_http_static_module ngx_http_static_init / / ngx_http_gzip_static_module ngx_http_gzip_static_init / / ngx_http_autoindex_module ngx_http_autoindex_init / / ngx_http_index_module ngx_http_index_init / / ngx_http_auth_basic_module ngx_http_auth_basic_init / / ngx_http_access_module ngx_http_access_init / / ngx_http_limit_conn_module ngx_http_limit_conn_init / / ngx_http_limit_req_module ngx_http_limit_req_init / / ngx_http_rewrite_module ngx_http_rewrite_init / / ngx_http_ssl_module ngx_http_ssl_init / / ngx_ Http_write_filter_module ngx_http_write_filter_init / / ngx_http_header_filter_module ngx_http_header_filter_init / / ngx_http_chunked_filter_module ngx_http_chunked_filter_init / / ngx_http_range_body_filter_module ngx_http_range_body_filter_init / / ngx_http_gzip_filter_module ngx_http_gzip_filter_init / / ngx_http_postpone_filter_module ngx_http_ Postpone_filter_init / / ngx_http_ssi_filter_module ngx_http_ssi_filter_init / / ngx_http_charset_filter_module ngx_http_charset_postconfiguration / / ngx_http_userid_filter_module ngx_http_userid_init / / ngx_http_headers_filter_module ngx_http_headers_filter_init / / ngx_http_copy_filter_module ngx_http_copy_filter_init / / ngx_http_range_body_filter_module Ngx_http_range_body_filter_init / / ngx_http_not_modified_filter_module ngx_http_not_modified_filter_init if (module- > postconfiguration (cf)! = ngx_ok) {return ngx_conf_error } / / initialize all variables / / not only the variables of the http core module / / but also the variables exported by other http modules, and the variables set by the set command in the configuration file / / the initialization here includes initializing the hash table and initializing the array index if (ngx_http_variables_init_vars (cf)! = ngx_ok) {return ngx_conf_error } / * * http {} 's cf- > ctx was needed while the configuration merging * and in postconfiguration process * / * cf = pcf; / / initialize phase_engine structure if (ngx_http_init_phase_handlers (cf, cmcf)! = ngx_ok) {return ngx_conf_error } / * optimize the lists of ports, addresses and server names * / / create a http connection and set it to the listening status if (ngx_http_optimize_servers (cf, cmcf, cmcf- > ports)! = ngx_ok) {return ngx_conf_error;} return ngx_conf_ok;failed: * cf = pcf; return rv;} / /}
In this function, the configuration structure is assigned and created for all http modules, and the corresponding initialization callback for each module is called.
Finally, call ngx_http_optimize_servers to create a http connection, join the listening array of cycle, and set it to the listening state.
The configuration of the http module in the nginx configuration file is divided into three layers: main, sever, and location. Therefore, the following six callback functions are defined in the http module context ngx_http_module_t to create and save configuration information:
Create_main_conf
Init_main_conf
Create_srv_conf
Merge_srv_conf
Create_loc_conf
Merge_loc_conf
In ngx_http_block, all six callback functions of ngx_http_module are looped to create the relevant configuration structure.
Analysis of server and location configuration-- ngx_http_core_server, ngx_http_core_location
After calling the create_main_conf, create_srv_conf and create_loc_conf of all http modules, all the modules that need to configure the structure have completed the creation of the configuration structure, so after calling the preconfiguration callback functions of all modules, the configuration parsing work is officially launched.
By calling the ngx_conf_parse function, the parsing of the http configuration block is started, and the corresponding function is called through the parsed command.
The ngx_command_t domain of the first ngx_http_module ngx_http_core_module contains a large number of configuration instructions, all of which appear in the http {} block, including two important instructions:
{ngx_string ("listen"), ngx_http_srv_conf | ngx_conf_1more, ngx_http_core_listen, ngx_http_srv_conf_offset, 0, null}, {ngx_string ("server"), ngx_http_main_conf | ngx_conf_block | ngx_conf_noargs, ngx_http_core_server, 0,0, null}, {ngx_string ("location"), ngx_http_srv_conf | ngx_http_loc_conf | ngx_conf_block | ngx_conf_take12, ngx_http_core_location Ngx_http_srv_conf_offset, 0, null}
The parsing functions ngx_http_core_listen, ngx_http_core_server and ngx_http_core_location of listen, server and location blocks are configured here.
Server Block parsing-- ngx_http_core_server
/ / static char * / / ngx_http_core_server (ngx_conf_t * cf, ngx_command_t * cmd, void * dummy) / / http server block resolution {static char * ngx_http_core_server (ngx_conf_t * cf, ngx_command_t * cmd, void * dummy) {char * rv; void * mconf; ngx_uint_t I; ngx_conf_t pcf Ngx_http_module_t * module; struct sockaddr_in * sin; ngx_http_conf_ctx_t * ctx, * http_ctx; ngx_http_listen_opt_t lsopt; ngx_http_core_srv_conf_t * cscf, * * cscfp; ngx_http_core_main_conf_t * cmcf; ctx = ngx_pcalloc (cf- > pool, sizeof (ngx_http_conf_ctx_t)) If (ctx = = null) {return ngx_conf_error;} http_ctx = cf- > ctx; ctx- > main_conf = http_ctx- > main_conf; / * the server {} 's srv_conf * / / allocate space for all http modules srv_conf ctx- > srv_conf = ngx_pcalloc (cf- > pool, sizeof (void *) * ngx_http_max_module); if (ctx- > srv_conf = = null) {return ngx_conf_error } / * the server {} 's loc_conf * / / allocate space storage loc_conf ctx- > loc_conf = ngx_pcalloc (cf- > pool, sizeof (void *) * ngx_http_max_module) to all http modules; if (ctx- > loc_conf = = null) {return ngx_conf_error;} / / iteratively call the create_srv_conf and create_loc_conf callbacks of each module to create a configuration structure for (I = 0 Ngx_ modules [I]; iModules +) {if (ngx_ modules [I]-> type! = ngx_http_module) {continue;} module = ngx_ modules [I]-> ctx; if (module- > create_srv_conf) {mconf = module- > create_srv_conf (cf); if (mconf = = null) {return ngx_conf_error } ctx- > srv_ confs [NGX _ modules [I]-> ctx_index] = mconf;} if (module- > create_loc_conf) {mconf = module- > create_loc_conf (cf); if (mconf = = null) {return ngx_conf_error;} ctx- > loc_ confs [NGX _ modules [I]-> ctx_index] = mconf }} / * the server configuration context * / cscf = ctx- > srv_ confession [NGX _ http_core_module.ctx_index]; cscf- > ctx = ctx; cmcf = ctx- > main_ confession [NGX _ http_core_module.ctx_index]; cscfp = ngx_array_push (& cmcf- > servers); if (cscfp = = null) {return ngx_conf_error;} * cscfp = cscf; / * parse inside server {} * / pcf = * cf; cf- > ctx = ctx Cf- > cmd_type = ngx_http_srv_conf; / / parse server block configuration rv = ngx_conf_parse (cf, null); * cf = pcf; / / if no port is listening, the default port 80 or 8000 if (rv = = ngx_conf_ok & &! cscf- > listen) {ngx_memzero (& lsopt, sizeof (ngx_http_listen_opt_t)); sin = & lsopt.u.sockaddr_in Sin- > sin_family = af_inet;#if (ngx_win32) sin- > sin_port = htons (80); # else sin- > sin_port = htons ((getuid () = = 0)? 80: 8000); # endif sin- > sin_addr.s_addr = inaddr_any; lsopt.socklen = sizeof (struct sockaddr_in); lsopt.backlog = ngx_listen_backlog; lsopt.rcvbuf =-1; lsopt.sndbuf =-1 # if (ngx_have_setfib) lsopt.setfib =-1 ngx_sock_ntop dif lsopt.wildcard = 1; (void) ngx_sock_ntop (& lsopt.u.sockaddr, lsopt.socklen, lsopt.addr, ngx_sockaddr_strlen, 1); if (ngx_http_add_listen (cf, cscf, & lsopt)! = ngx_ok) {return ngx_conf_error }} return rv;} / /}
Location Block parsing-- ngx_http_core_location
/ / static char * / / ngx_http_core_location (ngx_conf_t * cf, ngx_command_t * cmd, void * dummy) / / location block configuration resolution {static char * ngx_http_core_location (ngx_conf_t * cf, ngx_command_t * cmd, void * dummy) {char * rv; u_char * mod; size_t len; ngx_str_t * value, * name Ngx_uint_t I; ngx_conf_t save; ngx_http_module_t * module; ngx_http_conf_ctx_t * ctx, * pctx; ngx_http_core_loc_conf_t * clcf, * pclcf; ctx = ngx_pcalloc (cf- > pool, sizeof (ngx_http_conf_ctx_t)); if (ctx = = null) {return ngx_conf_error;} pctx = cf- > ctx Ctx- > main_conf = pctx- > main_conf; ctx- > srv_conf = pctx- > srv_conf; ctx- > loc_conf = ngx_pcalloc (cf- > pool, sizeof (void *) * ngx_http_max_module); if (ctx- > loc_conf = = null) {return ngx_conf_error;} for (I = 0; ngx_ modules [I]; iTunes +) {if (ngx_ modules [I]-> type! = ngx_http_module) {continue } module = ngx_ modules [I]-> ctx; if (module- > create_loc_conf) {ctx- > loc_ confession [NGX _ modules [I]-> ctx_index] = module- > create_loc_conf (cf); if (ctx- > loc_ confs [NGX _ modules [I]-> ctx_index] = null) {return ngx_conf_error } clcf = ctx- > loc_ confi [NGX _ http_core_module.ctx_index]; clcf- > loc_conf = ctx- > loc_conf; value = cf- > args- > elts; if (cf- > args- > nelts = = 3) {len = value [1] .len; mod = value [1] .data; name = & value [2]; if (len = 1 & & mod [0] = ='=) {clcf- > name = * name Clcf- > exact_match = 1;} else if (len = = 2 & & mod [0] = ='^'& & mod [1] = ='~') {clcf- > name = * name; clcf- > noregex = 1;} else if (len = = 1 & & mod [0] = ='~') {if (ngx_http_core_regex_location (cf, clcf, name, 0)! = ngx_ok) {return ngx_conf_error }} else if (len = = 2 & & mod [0] = ='~'& & mod [1] = ='*') {if (ngx_http_core_regex_location (cf, clcf, name, 1)! = ngx_ok) {return ngx_conf_error }} else {ngx_conf_log_error (ngx_log_emerg, cf, 0, "invalid location modifier\"% v\ ", & value [1]); return ngx_conf_error;}} else {name = & value [1]; if (name- > data [0] = ='=') {clcf- > name.len = name- > len-1; clcf- > name.data = name- > data + 1 Clcf- > exact_match = 1;} else if (name- > data [0] = ='^'& & name- > data [1] = ='~') {clcf- > name.len = name- > len- 2; clcf- > name.data = name- > data+ 2; clcf- > noregex = 1;} else if (name- > data [0] = ='~') {name- > len--; name- > data++ If (name- > data [0] = ='*') {name- > len--; name- > data++; if (ngx_http_core_regex_location (cf, clcf, name, 1)! = ngx_ok) {return ngx_conf_error }} else {if (ngx_http_core_regex_location (cf, clcf, name, 0)! = ngx_ok) {return ngx_conf_error;}} else {clcf- > name = * name; if (name- > data [0] = ='@') {clcf- > named = 1 } pclcf = pctx- > loc_ confs [NGX _ http_core_module.ctx_index]; if (pclcf- > name.len) {/ * nested location * / # if 0 clcf- > prev_location = pclcf # endif if (pclcf- > exact_match) {ngx_conf_log_error (ngx_log_emerg, cf, 0, "location\" v\ "cannot be inside"the exact location\"% v\ "", & clcf- > name, & pclcf- > name); return ngx_conf_error } if (pclcf- > named) {ngx_conf_log_error (ngx_log_emerg, cf, 0, "location\" v\ "cannot be inside"the named location\"% v\ "", & clcf- > name, & pclcf- > name); return ngx_conf_error } if (clcf- > named) {ngx_conf_log_error (ngx_log_emerg, cf, 0, "named location\" v\ "can be"on the server level only", & clcf- > name); return ngx_conf_error;} len = pclcf- > name.len # if (ngx_pcre) if (clcf- > regex = = null & & ngx_filename_cmp (clcf- > name.data, pclcf- > name.data, len)! = 0) # else if (clcf- > name.data, pclcf- > name.data, len)! = 0) # endif {ngx_conf_log_error (ngx_log_emerg, cf, 0, "location\"% v\ "is outside location\"% v\ "" & clcf- > name, & pclcf- > name) Return ngx_conf_error;}} / / add the location configuration to the locations configuration list if (ngx_http_add_location (cf, & pclcf- > locations, clcf)! = ngx_ok) {return ngx_conf_error;} save = * cf; cf- > ctx = ctx; cf- > cmd_type = ngx_http_loc_conf; rv = ngx_conf_parse (cf, null); * cf = save; return rv } / /}
Similar to the server block parsing function ngx_http_core_server, it creates loc_conf for all modules, and in order to prevent the inner and outer layers from having the same instruction, it is merged through the merge function after the configuration assignment is completed.
However, unlike the server block, the location block specifies the application uri of the location configuration through a path or regular expression after the location, so the location command is parsed by calling pcre in the ngx_http_core_location function.
When the parsing is complete, call ngx_http_add_location to add the parsing result to the locations linked list.
/ / ngx_int_tngx_http_add_location (ngx_conf_t * cf, ngx_queue_t * * locations,// ngx_http_core_loc_conf_t * clcf) / / add the location configuration to the locations configuration list {{ngx_int_tngx_http_add_location (ngx_conf_t * cf, ngx_queue_t * * locations, ngx_http_core_loc_conf_t * clcf) {ngx_http_location_queue_t * lq If (* locations = = null) {* locations = ngx_palloc (cf- > temp_pool, sizeof (ngx_http_location_queue_t)); if (* locations = = null) {return ngx_error;} ngx_queue_init (* locations);} lq = ngx_palloc (cf- > temp_pool, sizeof (ngx_http_location_queue_t)); if (lq = null) {return ngx_error } if (clcf- > exact_match#if (ngx_pcre) | | clcf- > regex#endif | | clcf- > named | | clcf- > noname) {lq- > exact = clcf; lq- > inclusive = null;} else {lq- > exact = null; lq- > inclusive = clcf;} lq- > name = & clcf- > name; lq- > file_name = cf- > conf_file- > file.name.data; lq- > line = cf- > conf_file- > line; ngx_queue_init (& lq- > list) Ngx_queue_insert_tail (* locations, & lq- > queue); return ngx_ok;} / /}}
The configuration structure after all the configuration parsing is completed.
This is the end of the article on "how to configure HTTP protocol-related modules in Nginx". Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "how to configure HTTP protocol-related modules in Nginx". If you want to learn more knowledge, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.