Ngnx works

Nginx is a lightweight HTTP service program, compared to other server programs such as Apache, Nginx take up less memory, high stability, strong concurrent processing capability. Meanwhile Nginx is a reverse proxy service program, and mail proxy service program. Nginx has a rich library of modules, flexible configuration, low consumption of resources and so on. Here, we work together in-depth look at the mechanism of Nginx

1. How to achieve high performance Nginx low consumption of it?

We illustrate the following aspects:

Network event handling mechanism
  • Nginx asynchronous non-blocking mode processing request, the request can handle thousands of simultaneous

  • Nginx supports select / epoll other popular event handling mechanism, the system is automatically selected according to the environment

  • Nginx independent event handling mechanism of the system, can efficiently process requests

Resource allocation techniques
  • Nginx phased resource allocation techniques, such that its CPU and memory consumption is very low

Multi-core processing optimization
  • Nginx default multi-process startup mode

  • Nginx process and contains the Master Worker process

  • SMP can take full advantage of symmetric multi-processing, reduce congestion Worker process disk I / O's

  • Nginx Support Worker process and the CPU core binding one correspondence, to avoid process context switches cause cache misses
    on mentioned technologies and optimized Nginx many places above, make it the fastest Nginx HTTP server.

2.Nginx process model

Nginx technology in architecture, the process model is a crucial part. Next, we take a look at Nginx process model, as well as their working mechanism.

Linux system, Nginx default to start the daemon daemon mode, the default multi-process mode. Nginx includes two types of processes:

  • Master course, the number is only one, Nginx management process itself and the Worker

  • Worker process, and the number is generally equal the number of CPU cores, all Nginx request processing, are completed in the process of Worker

Here, we are in-depth look at the Master and Worker process.

2.1 Master process mechanism

When Nginx start, Master process creation, is responsible for initializing Nginx and related modules, fork Worker process of receiving and processing work outside signals.

Nginx initialization process:

  • Parsing the configuration file, which is the most important link initialization Nginx

  • Each configuration instruction to call the callback function to complete the configuration of each module, and the like interconnected

  • The establishment listen socket (listenfd)

  • After the preparations are completed, fork worker child processes and cache sub-process
    Master process signal handling mechanism
    we send a signal to Nignx Master process by the kill command to see how Master process handling:




Analysis process:

  • Master process receives a HUP signal

  • Master process to reload configuration files

  • Master Worker process starts a new process

  • Master process sends a signal to the Worker process

  • Old Worker process no longer receive new request

  • Old Worker process finishes with the current request to exit

  • So far, Nginx complete GR

Note: Nginx 0.8 version in the future, provided the -s parameter to stop and restart the service management Nginx, note line 11:




2.2 Worker Process mechanism

Worker process is responsible for processing all requests, we passed a HTTP request to sort out what the Worker workflow:

  • The new request comes in: All Work processes will become readable listenfd

  • Actually grab mutex: All Worker process prior to registration listenfd reading event, first grab accept_mutex

  • Grab mutex Worker, registered listenfd read event, call accept in the event to accept the connection

  • After the get request, the Worker process starts reading request, parses the request, processing the request, generating the data, and then returned to the client

  • Worker process Disconnect

Note: An HTTP request is handled entirely by Worker process, but only in a deal with the Worker

Advantage 2.3 Master-Worker process architecture mechanisms What? ?

For each Worker process, a separate process, no lock, the lock-saving resource overhead caused; between worker processes without disturbing each other, GR is a good example, uninterrupted service.

2.4 Network event handling mechanism

Nginx uses a non-blocking asynchronous event handling mechanism in support of select / poll / epoll / kqueue and so on. Nginx can simultaneously monitor multiple events, calling them is blocked. But there are calls timeout within the timeout period, if there is an event ready, returns, or back into the epoll. When the write returns EAGAIN, the event will once again be placed in the epoll.

Only one processing thread requests processed simultaneously, only a so-called multiple requests concurrently, just keep switching request only. Although the switch, but the switch does not involve a context switch, very lightweight compared. More concurrency, but it will take up more memory.

Related to the process as well, and timer signals, in addition to explain this part alone.

3. Nginx module which contains

Nginx is a modular service architecture, rich set of modules, loosely coupled, let Nginx more powerful! I look at what are Nginx module

  • Kernel module
    implements the underlying communication protocol, to build the operating environment for other modules / processes, collaboration infrastructure, open listen port, start the worker process

  • HTTP / Mail module
    two special modules, and the kernel module located between functional modules; at the top of the kernel module implements another layer of abstraction; processing HTTP / MAIL protocol events; be sure to call the function module in the correct order.

  • Event module is
    responsible for establishing the connection after listening accept, read and write add and delete events; used in conjunction with non-blocking I / O model; support for select / poll / epoll / kqueue etc; note thundering herd effect, followed by interpretation.

  • Handler module is
    responsible for receiving client requests and to generate an output; configuration content handler module configuration file location directive.

  • Filter module is
    responsible for processing the output of the content, modifying the content of the output; Fiter module until after obtaining the reply message, sending a response to the user, executes the processing operation; call sequence is determined at compile time.

  • Upstream module
    to achieve the function of a reverse proxy, forwards the request to the responsible back-end server, and reads the response back to the client; single restriction across the complete network data receiving, processing and forwarding;

  • LoadBalancer module
    specified depending on the configuration algorithm, choose between a variety of back-end server in a completed request forwarding server; what are the algorithms it?

Thundering herd effect:

    • When the kernel accept a connection, it will wake up the process of waiting for all

    • But in fact only one process can get a connection, other processes are being awakened invalid

    • So Nginx uses its own set of accept locking mechanism to prevent multiple processes at the same time calling accept

    • Nginx lock multi-process is achieved by CPU spin lock at the bottom by default. If the operating system does not support spin lock on the use of file locking.

Guess you like

Origin www.cnblogs.com/xingxia/p/nginx_work_principle.html