Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How Nginx works

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

How Nginx Works

Nginx consists of a kernel and modules.

Nginx itself does very little work. When it receives an HTTP request, it simply maps the request to a location block by looking up the configuration file, and the instructions configured in this location will start different modules to complete the work, so the modules can be regarded as Nginx real labor workers.

Usually the instructions in a location involve a handler module and multiple filter modules (of course, multiple locations can reuse the same module). The handler module is responsible for processing the request and completing the generation of the response content, while the filter module processes the response content. The modules developed by users according to their own needs belong to third-party modules. It is with the support of so many modules that Nginx's functions will be so powerful.

Nginx modules are structurally divided into core modules, basic modules and third-party modules:

Core modules: HTTP module, EVENT module and MAIL module

Basic modules: HTTP Access module, HTTP FastCGI module, HTTP Proxy module and HTTP Rewrite module;

Third-party modules: HTTP Upstream Request Hash module, Notice module and HTTP Access Key module.

Nginx modules are functionally divided into three categories:

Handlers: These modules process requests directly and perform operations such as outputting content and modifying header information. Handlers processor module generally can only have one; Filters (filter module): This kind of module mainly modifies the content output by other processor modules, and finally is output by Nginx; Proxies (proxy module): This kind of module is Nginx HTTP Upstream module, these modules mainly interact with some backend services such as FastCGI, etc., to achieve service proxy and Load Balancer and other functions.

Nginx process model In terms of working mode, Nginx is divided into two modes: single working process and multiple working process.

In the single-worker mode, in addition to the main process, there is also a worker process, which is single-threaded;

In multi-worker mode, each worker contains multiple threads. Nginx defaults to single-worker mode.

When Nginx starts, it has a master process and multiple worker processes.

The master process is mainly used to manage the worker process, mainly including: receiving signals from the outside world, sending signals to each worker process, monitoring the running status of the worker process, and automatically restarting the new worker process when the worker process exits (in abnormal circumstances). The master process acts as the interface between the entire process group and the user, and also as a guardian of the process. It does not need to handle network events, and is not responsible for business execution. It only manages the worker process to achieve functions such as restarting services, smoothly upgrading, replacing log files, and taking effect of configuration files in real time.

3.How Nginx +FastCGI works

Nginx does not support direct calls to external programs or parsing, all external programs (including PHP) must be called through the FastCGI interface. FastCGI is a socket under Linux (this socket can be a file socket or an ip socket). wrapper In order to call CGI programs, you also need a FastCGI wrapper (wrapper can be understood as a program used to start another program), which is bound to a fixed socket, such as a port or file socket. When Nginx sends a CGI request to this socket, the wrapper receives the request through the FastCGI interface, and then Fork(derived) a new thread that calls the interpreter or external program to process the script and read the returned data; then the wrapper passes the returned data to Nginx along the fixed socket through the FastCGI interface; finally Nginx sends the returned data (html page or image) to the client.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report