This is the 22nd day of my participation in Gwen Challenge

background

Nginx is based on the C language, and the relevant modular structure is integrated here.

Code implementation

Basic data structure

The author of Nginx has implemented many nginx-style data structures and public functions in pursuit of extreme efficiency. For example, Nginx provides strings with length, and the string copy function ngx_copy is optimized according to compiler options. Therefore, when writing Nginx modules, we should try to call Nginx provides a PI, although some apis are just macro definitions of glibc

Modular construction

  • The internal structure of Nginx is composed of Nginx Core and a series of functional modules
  • Nginx provides the basic functions of the Web server, as well as the Web service reverse proxy and Email service reverse proxy functions
  • Nginx core implements the underlying communication protocol, builds the basic runtime environment for other modules and Nginx processes, and builds the basis for other modules to collaborate
  • Most of the protocol – or application-specific functionality is implemented in modules

Module chain

  • Nginx organizes functional modules into a chain, and when a request arrives, it passes through some or all of the modules along the chain for processing
  • Each module implements specific functionality, such as a module to decompress requests, a module to implement SSI, a module to communicate with upstream servers, and a module to communicate with FastCGI services
  • The HTTP module and the Mail module are special in that they sit in the middle of the Nginx core and other functional modules in Nginx Another layer of abstraction is implemented on top of core, which handles events related to HTTP and Email protocols (SMTP/POP3/IMAP) and ensures that these events can be invoked in the correct order by other functional modules

Classification of modules

Nginx modules can be classified into the following types according to their functions:

  • event module: Build the framework of event processing mechanism independent of the operating system, and provide the processing of each specific event, including ngx_eventS_module, ngX_EVENT_core_module and ngX_epoll_module,Nginx specific use of event processing module, which depends on the specific operation Make system and compile options
  • Phase Handler: This type of module, also known as the Handler module, is responsible for processing the client request and generating the response content. For example, the ngx_HTTP_STATIC_module module handles the client’s static page request and prepares the corresponding disk file for output as the response content
  • Output filter: also known as the filter module, is mainly responsible for processing the output content, can modify the output, for example, can realize the output of all HTML pages to add predefined footbar, or to replace the URL of the output picture and other work
  • Upstream: The Upstream module acts as a reverse proxy, forwarding the actual request to the back-end server and reading the response back to the client. The Upstream module is a special handler that reads the response from the back-end server rather than actually generating it
  • Load-balancer: a load balancing module that implements a specific algorithm to select one of the many back-end servers as the forwarding server for a request

Worker processes

Nginx uses a multi-process model to provide external services: one master process and multiple worker processes. The master process is responsible for managing Nginx and other worker processes. All actual business processing logic is completed in the worker process, and the worker process has an ngx_worke The r_process_cycle() function, which executes an infinite loop, processing incoming requests from clients and processing them until the entire Nginx service is stopped

The worker process processes a request as follows:

  • Mechanisms provided by the operating system (such as epoll, Kqueue, etc.) generate related events
  • These events are received and processed, and if data is received, a higher-level Request object is generated
  • Process the header and body of the request
  • Generates a response and sends it back to the client
  • The request is processed
  • Reinitializes the timer and other events

phase handler

  • Phase Handlers are a handler for a processing phase
  • Each stage may contain several handlers
  • When the HTTP Request reaches a certain stage, the handler of this stage is called to process the HTTP Request
  • Typically, a phase handler processes the request and produces some output
  • Typically a Phase handler is associated with a location defined in a configuration file

A phase handler typically performs the following tasks:

  • Getting the Location configuration
  • Generate the appropriate response
  • Send the response headers
  • Send the response body