Nginx Source Analysis--http Module

Source: Internet
Author: User

Source: Nginx 1.12.0 First, Nginx HTTP module Introduction Due to the performance advantages of nginx, there are more and more units, individuals using Nginx or openresty, Tengine and other derivative version as a Web server, load balancer Server, security gateway to use. In these scenarios, the dependency is the Nginx HTTP module, nginx designers use modular design ideas, allowing users to add their own design modules at various stages of HTTP request processing to implement their own logic, to expand some functions. second, the HTTP module function IntroductionThe rich functionality of the HTTP module is actually implemented by a http_module, each module provides a separate function to facilitate development and maintenance. Nginx through the configuration file configuration item settings to implement the function of the relevant module calls, the following example:
HTTP {     server {location          ~ \.php$ {               #proxy_pass命令在http_proxy_module中的被ngx_http_proxy_pass函数实现               proxy _pass http://127.0.0.1;}}}     
The HTTP module processes a complete HTTP request as follows: In the above process, Http_handler, Output_filter, respectively, is the execution of the Phase_handler, filter two types of HTTP module location.     Before you talk about these two types of modules, you need to clarify how the modules are loaded into the nginx. At nginx startup, each module is loaded into an array, then called ngx_parse_conf to find a specific type of command in the configuration file, and then the module that contains the command is found by command.     and execute the command corresponding function (these functions are usually set some variables in the configuration structure of the corresponding command or assign the function to the related handler function pointers, so that they can be called directly when processing the corresponding command request). When dealing with the command of HTTP, the corresponding function initializes all modules of type Http_core_module, with the following process: third, phase handlers request processing stage     nginx request processing of HTTP is divided into Post_read, Server_rewrite, Find_config, REWRITE, Post_rewrite, Preaccess, ACCESS, post_access, Try_files, CONTENT, log these 11 stages, except Find_config, Post_rewrite, post_access, Try_ Files These 4 stages the user cannot add a custom handler function, and each of the remaining stages initializes an array with the Ngx_http_init_phases function to hold the handler pointer for this stage. As follows:
Nginx/src/http/ngx_http.c////////static ngx_int_tngx_http_init_phases (ngx_conf_t *cf, Ngx_http_core_main_ conf_t *cmcf) {//Application pointer array to store the handler pointer for that stage if (Ngx_array_init (&cmcf->phases[ngx_http_post_read_phase].handle    RS, Cf->pool, 1, sizeof (NGX_HTTP_HANDLER_PT)) = NGX_OK) {return ngx_error; } ..... return NGX_OK;} Nginx/src/http/modules/ngx_http_rewrite_module.c///////The function is called in the postconfiguration phase of rewrite static Ngx_int_ Tngx_http_rewrite_init (ngx_conf_t *cf) {...//get rewrite command corresponding CORE_MODULE configuration structure CMCF = Ngx_http_conf_get_module_ma      in_conf (cf, Ngx_http_core_module); In the array pointed to by &cmcf->phases[ngx_http_server_rewrite_phase].handlers, request a function pointer space h = ngx_array_push (&cmcf->    Phases[ngx_http_server_rewrite_phase].handlers);    if (h = = NULL) {return ngx_error;  }//Add the function to the array in the newly requested element *h = Ngx_http_rewrite_handler; ... return NGX_OK;} Nginx/src/http/ngx_http.cIterate through each phase, add the relevant checker function to each phase, and add the functions registered in each phase stage uniformly to a phase+ in the order of the Phase_engine array index, each phase The handler function is identified by a unique index, which facilitates subsequent traversal using the static ngx_int_tngx_http_init_phase_handlers (ngx_conf_t *CF, Ngx_http_core_main_        conf_t *cmcf) {for (i = 0; i < ngx_http_log_phase; i++) {h = cmcf->phases[i].handlers.elts; switch (i) {case ngx_http_server_rewrite_phase:if (Cmcf->phase_engine.server_rewrite_index = = (ngx_            uint_t)-1) {cmcf->phase_engine.server_rewrite_index = n;            }//According to the phase stage, the corresponding checker checker = Ngx_http_core_rewrite_phase of each stage are determined;        Break        ..... default:checker = Ngx_http_core_generic_phase;        } n + = cmcf->phases[i].handlers.nelts;  Iterate through the registration functions in each phase handlers array and add them uniformly to the cmcf->phase_engine.handlers for (j = cmcf->phases[i].handlers.nelts-1; J >=0;            j--) {ph->checker = checker; Ph->handler = H[j];            Ph->next = n;        ph++; }} return NGX_OK;}
      When viewing module source code, we found some modules that did not register the Postconfiguration function, which means that these modules did not pass Ngx_array_ The push function adds handler to the corresponding phase, such as Http_memcached_module. However, these modules assign real handler to the handler pointer of the conf structure corresponding to the command location by command-corresponding function, which are handler pointers in Ngx_http_update_location_ The config function (which is called by Ngx_http_core_find_config_phase) is assigned to R->content_handler, and then in Http_core_content_ The phase function was executed (but also skipped the other handler in the phase). It can be seen that these have not been shown by Ngx_array_push to join a phase handler, all in this way to join the content_phase.
Nginx/src/http/ngx_http_core_module.c/////voidngx_http_update_location_config (ngx_http_request_t *r) {    ngx_http_core_loc_conf_t *CLCF;    CLCF = ngx_http_get_module_loc_conf (R, Ngx_http_core_module);    ....//if the corresponding command has a direct handler function, call the IF (clcf->handler) {R->content_handler = clcf->handler; }} ngx_int_tngx_http_core_content_phase (ngx_http_request_t *r, ngx_http_phase_handler_t *ph) {.... if (R->cont        Ent_handler) {r->write_event_handler = Ngx_http_request_empty_handler;        If present, execute the handler function directly and skip the other processing functions of the content stage Ngx_http_finalize_request (R, R->content_handler (r));    return NGX_OK; }    .....} Call each of the functions in all phase to complete the processing of the request voidngx_http_core_run_phases (ngx_http_request_t *r) {... while (ph[r->phase_ Handler].checker) {//based on the phase index in the request determines the checker function that is executed//checker function determines whether to end processing or continue next phase handler R based on processing results c = Ph[r->phase_handler].checker (R, &ph[r->phase_handler]);        if (rc = = NGX_OK) {return; }    }}
Combining the above two request Processing module processing method, if necessary in a specific phase phase, the use of Ngx_array_push this way; R->content_handler this way only in the content Phase phase execution, and other handler in the stage are not able to execute, which requires the developer's consideration. Four, filter output processing moduleFilter output processing is mainly in the output when the output is processed, the filter handler call order and phase handler type (according to the phase stage order), the filter type of each handler is linked by the form of a link to a block, Only the list header Ngx_http_top_body_filter function pointers are global. In the functionin Ngx_http_output_filter, there is a call to filter handler, which can be called in a normal ngx_http_send_response function, or in a specific phase Called in the handler.      when each module is initialized, the value of the Ngx_http_top_body_filter pointer is saved to the Ngx_http_next_body_filter local variable, The current filter's handler function is then assigned to Ngx_http_top_body_filter, and the value of Ngx_http_next_body_filter is assigned to Ctx->output_ in each filter's handler function. Filter so that the individual filter can be traversed sequentially.       No ngx_http_next_body_ for Ngx_http_write_filter_module, ngx_http_header_filter_module two modules The filter variable is because Ngx_http_write_filter_module is the last filter module and therefore does not have next. Ngx_http_header_filter_module is the second-to-last module, but the Write_filter function is called in the filter function, so there is no use of next.
/////nginx/src/http/ngx_http_core_module.c///////ngx_int_tngx_http_output_  Filter (ngx_http_request_t *r, ngx_chain_t *in) {...//through the link list interface one at a time traverse each filter module for processing rc = Ngx_http_top_body_filter (R,    in); ..... return RC;} Nginx/src/http/ngx_http_copy_filter_module.c////////////is called in the Postconfiguration stage, initializing the filter handler list static    Ngx_int_tngx_http_copy_filter_init (ngx_conf_t *cf) {ngx_http_next_body_filter = Ngx_http_top_body_filter;     Ngx_http_top_body_filter = Ngx_http_copy_filter; return NGX_OK;}    This stage outputs the next filter handler function static Ngx_int_tngx_http_copy_filter (ngx_http_request_t *r, ngx_chain_t *in) in the chain list {.....        ..... ngx_http_set_ctx (R, CTX, Ngx_http_copy_filter_module);        Record the next filter ctx->output_filter = (ngx_output_chain_filter_pt) ngx_http_next_body_filter to be executed;        Ctx->filter_ctx = R;    ... rc = Ngx_output_chain (CTX, in); ..... return RC;} 

Nginx Source Code Analysis--http module

Related Article

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.