May 23, 2021 Nginx Getting started
Nginx uses a multi-process model to provide services to the outside world, one master process, and multiple worker processes. The master process is responsible for managing the Nginx itself and other worker processes.
All the actual business processing logic is in the worker process. There is a function in the worker process that performs an infinite loop, constantly processing incoming requests from the client and processing them until the entire Nginx service is stopped.
In the worker process, ngx_worker_process_cycle () function is the handler of this infinite loop. In this function, the simple processing of a request is as follows:
To give you a better understanding of the request processing process in Nginx, let's take HTTP Request as an example to explain in detail.
From Nginx's internal perspective, the processing of an HTTP Request involves the following stages.
Here, we need to understand the concept of phase handler. P hase literally means stage. So it's good to understand that there are some handlers that contain several processing phases.
At each stage, there are several handlers, and then when you process to a stage, the handler of that stage is called in turn to process HTTP Request.
Typically, a phase handler processes this request and produces some output. Typically, the phase handler is associated with a location defined in the configuration file.
A phase handler typically performs the following tasks:
When Nginx reads to the header of an HTTP Request, Nginx first looks for the configuration of the virtual host associated with the request. If the configuration of this virtual host is found, this HTTP Request will typically go through several stages of processing:
During the content generation phase, in order to produce the correct response to a request, Nginx must give the request to a suitable content handler to handle. I f the location for this request is explicitly specified as a content handler in the configuration file, then Nginx can find the corresponding handler directly by matching the location and leave this request to the content handler for processing. Such configuration instructions include things like perl, flv, proxy_pass, mp4, etc.
If a request corresponds to a location that does not have a directly configured content handler, Nginx tries in turn:
.gz
look for the presence of a corresponding .gz file, and if so, send this to the client (in the case of gzip supported by the client).
When the content generation phase is complete, the resulting output is passed to the filter module for processing. T he filter module is also location-related. A ll fiter modules are organized into a chain. The output crosses all the filters in turn until there is a return value for the filter module indicating that processing is complete.
Here are a few common filter modules, such as:
Of all the filters, there are a few filter modules to look at. This is described in order of call: