Detailed analysis of each stage of nginx's http request processing

Detailed analysis of each stage of nginx's http request processing

When writing the HTTP module of nginx, it is necessary to process the HTTP requests accordingly at each stage to achieve different purposes, such as whether there is access permission when the request is initiated, filtering or other processing when the content is generated, etc. If the processing phase registered in the compiled nginx module is incorrect, the desired result may not be achieved, for example, the content you want to process is not actually available at this time, and so on.

Multiple phase types are defined within nginx to meet different processing requirements (in ngx_http_core_module.h, different versions are different):

typedef enum {
 NGX_HTTP_POST_READ_PHASE = 0,
 
 NGX_HTTP_SERVER_REWRITE_PHASE,
 
 NGX_HTTP_FIND_CONFIG_PHASE,
 NGX_HTTP_REWRITE_PHASE,
 NGX_HTTP_POST_REWRITE_PHASE,
 
 NGX_HTTP_PREACCESS_PHASE,
 
 NGX_HTTP_ACCESS_PHASE,
 NGX_HTTP_POST_ACCESS_PHASE,
 
 NGX_HTTP_TRY_FILES_PHASE,
 NGX_HTTP_CONTENT_PHASE,
 
 NGX_HTTP_LOG_PHASE
} ngx_http_phases;

The corresponding meanings are:

NGX_HTTP_POST_READ_PHASE = 0 //Read request phase NGX_HTTP_SERVER_REWRITE_PHASE //URI conversion phase NGX_HTTP_FIND_CONFIG_PHASE //Find the corresponding configuration to execute the phase NGX_HTTP_REWRITE_PHASE //URI conversion phase (not very clear here)
NGX_HTTP_POST_REWRITE_PHASE //The stage for processing the converted URL result NGX_HTTP_PREACCESS_PHASE //Permission check preparation stage NGX_HTTP_ACCESS_PHASE //Permission check stage NGX_HTTP_POST_ACCESS_PHASE //Permission check result processing stage NGX_HTTP_TRY_FILES_PHASE //Processing the try_files stage in the configuration NGX_HTTP_CONTENT_PHASE //Processing the generated return data stage (not considered too detailed here, of course, filters can be ignored)
NGX_HTTP_LOG_PHASE //Record the log processing phase, which should be processed after the request is completed and when the request is closed

From this configuration, we can analyze the entire process of nginx in processing requests. The process is executed from beginning to end. It can be seen that LOG is executed at the end. The processing of content segments is generally done in the filter module. The processing segment registered in the NGX_HTTP_LOG_PHASE stage cannot obtain the returned data. The returned data is directly released after being sent to the client. Therefore, when processing each stage, the data preparation status of each stage should be clear.

Normally, we can register our own processing module in the following way:

static ngx_int_t
ngx_http_xxx_init(ngx_conf_t *cf)
{
 ngx_http_handler_pt *h;
 ngx_http_core_main_conf_t *cmcf;
 
 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
 
 h = ngx_array_push(&cmcf->phases[NGX_HTTP_CONTENT_PHASE].handlers);
 if (h == NULL) {
 return NGX_ERROR;
 }
 
 *h = ngx_http_xxx_handler;
 
 return NGX_OK;
}

And the return value of ngx_http_xxx_up_handler can only be the following:

NGX_OK //Processing successful, enter the next stageNGX_DECLINED //Abandon processingNGX_AGAIN || NGX_DONE //Processing completed, returning this value will trigger a requestNGX_ERROR || NGX_HTTP_.. //Processing error or other status values ​​of HTTP

In addition, for the NGX_HTTP_CONTENT_PHASE phase, there is actually another way to register, Just like this:

static char *
ngx_http_xxx_server(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
 ngx_str_t *value;
 ngx_url_t u;
 ngx_http_core_loc_conf_t *clcf;
 
 clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
 
 clcf->handler = ngx_http_xxx_handler;
 
 if (clcf->name.data[clcf->name.len - 1] == '/') {
 clcf->auto_redirect = 1;
 }
 
 return NGX_CONF_OK;
}

However, this way, you have to do too much. In most cases, you need to consider upstream integration or special processing of requests. For example, for distributed storage distribution, it is necessary to associate request processing with the file system, such as directly handing the requested data to a special SERVER to get the content. hehe.

This concludes this article on the detailed analysis of each stage of nginx's http request processing. For more information on nginx's http request processing, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Nginx implements https website configuration code example
  • Detailed tutorial on configuring nginx for https encrypted access
  • Implementation of Nginx domain name forwarding https access
  • Alibaba Cloud Nginx configures https to implement domain name access project (graphic tutorial)
  • Nginx configures the same domain name to support both http and https access
  • Detailed configuration of Nginx supporting both Http and Https
  • Use nginx + secondary domain name + https support
  • Nginx handles http request implementation process analysis

<<:  Detailed analysis of the syntax of Mysql update to modify multiple fields and

>>:  Markup Languages ​​- Lists Again

Recommend

Detailed example of MySQL (5.6 and below) parsing JSON

MySQL (5.6 and below) parses json #json parsing f...

VUE implements token login verification

This article example shares the specific code of ...

Comprehensive summary of Vue3.0's various listening methods

Table of contents Listener 1.watchEffect 2.watch ...

How to install Nginx in Docker

Install Nginx on Docker Nginx is a high-performan...

MySQL Series 13 MySQL Replication

Table of contents 1. MySQL replication related co...

Using docker command does not require sudo

Because the docker daemon needs to bind to the ho...

Detailed tutorial on installing ElasticSearch 6.4.1 on CentOS7

1. Download the ElasticSearch 6.4.1 installation ...

iframe parameters with instructions and examples

<iframe src=”test.jsp” width=”100″ height=”50″...

Advantages and disadvantages of common MySQL storage engines

Table of contents View all storage engines InnoDB...

Detailed explanation of using MySQL where

Table of contents 1. Introduction 2. Main text 2....

How to clean up data in MySQL online database

Table of contents 01 Scenario Analysis 02 Operati...