Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Nginx's modular architecture


May 23, 2021 Nginx Getting started


Table of contents


Nginx's modular architecture

The internal structure of Nginx consists of a core section and a series of functional modules. T his division is designed to make each module relatively simple, easy to develop, but also easy to extend the function of the system. For ease of description, we'll use Nginx core below to refer to the core functional parts of Nginx.

Nginx provides the underlying functionality of a Web server, as well as the reverse proxy capabilities of the Web service, the Email service reverse proxy. N ginx core implements the underlying communication protocol, builds a basic runtime environment for other modules and Nginx processes, and builds a collaborative foundation for other modules. In addition, or most protocol-related, or application-related functionality is implemented in these modules.

Overview of the module

Nginx organizes the functional modules into a chain, and when a request arrives, the request passes through some or all of the modules on the chain in turn. E ach module implements specific functions. For example, implement modules that extract requests, modules that implement SSI, modules that communicate with upstream servers, modules that communicate with FastCGI services.

There are two modules that are special, and they are in the middle of Nginx core and each function module. T hese two modules are the http module and the mail module. These two modules implement another layer of abstraction on top of Nginx core, handling events related to http protocols and Email-related protocols (SMTP/POP3/IMAP) and ensuring that these events are called to other functional modules in the correct order.

The HTTP protocol is currently implemented in the http module, but it is possible to be split into a separate module in the future to extend Nginx's support for SPDY protocols.

The classification of the module

Nginx modules can be broadly divided into the following types depending on their functionality:

  • event module: A framework for event handling mechanisms independent of the operating system is established, and the handling of specific events is provided. I ncludes ngx_events_module, ngx_event_core_module, ngx_epoll_module, etc. Nginx uses exactly which event handling module, depending on the specific operating system and compilation options.

  • Phase handler: This type of module is also directly referred to as the handler module. Primarily responsible for processing client requests and generating to-respond content, such as the ngx_http_static_module module, which handles static page requests from clients and prepares the corresponding disk files for output of the response content.

  • output filter: Also known as the filter module, it is primarily responsible for processing the output and can be modified. For example, you can do work like adding a predefined footbar to all html pages of the output, or replacing the URL of the output picture.

  • upstream: The upstream module implements the function of a reverse proxy, forwarding real requests to the back-end server, reading the response from the back-end server and sending them back to the client. The upstream module is a special handler, except that the response content is not actually generated by itself, but is read from the back-end server.

  • load-balancer: Load balancing module that implements a specific algorithm and selects one server out as the forwarding server for a request among the many back-end servers.