current position:Home>You can't even tell how nginx forwarded the request to you. It's good to say that you're not a crud Engineer?

You can't even tell how nginx forwarded the request to you. It's good to say that you're not a crud Engineer?

2022-06-24 09:01:22androidstarjack

Click on the top “ Terminal R & D department

 Set to “ Star standard ”, Master more database knowledge with you 

source :http://yg1.top/MlsaGk

One 、Nginx working principle

Two 、Nginx Process model

3、 ... and 、Nginx Handle HTTP Request flow

Nginx working principle

Nginx It consists of kernel and module ,Nginx There is very little work done by itself , When it receives a HTTP When asked , It simply maps the request to a location block.

this location The instructions configured in will start different modules to complete the work , So modules can be seen as Nginx A real worker .

Usually a location The instructions in refer to a handler Modules and multiple filter modular ( Of course , Multiple location Can reuse the same module ).

handler Module is responsible for processing requests , Complete the generation of response content , and filter Module processes the response content .

The modules developed by users according to their own needs belong to the third-party modules , It's supported by so many modules ,Nginx That's how powerful . WeChat search Terminal R & D department obtain 2021 Interview book of

Nginx The structure of the module is divided into core modules 、 Basic module and third-party module :

  • Core module :HTTP modular 、EVENT Module and MAIL modular

  • Basic module :HTTP Access modular 、HTTP FastCGI modular 、HTTP Proxy Module and HTTP Rewrite modular

  • Third-party module :HTTP Upstream Request Hash modular 、Notice Module and HTTP Access Key modular .

Nginx The modules are divided into the following three categories :

  • Handlers( Processor module ). Such modules directly handle requests , And make output and modification headers Information and other operations .Handlers Generally, there can only be one processor module .

  • Filters ( Filter module ). These modules mainly modify the output of other processor modules , Finally by Nginx Output .

  • Proxies ( Agent module ). These modules are Nginx Of HTTP Upstream Modules like that , These modules are mainly related to back-end services such as FastCGI Etc , Realize the functions of service agent and load balancing .

Let's use a picture to show :

 306f073e9f6f7313417a8de7a144de34.png


Nginx Process model

Nginx The default mode is multi process ,Nginx After starting , Will run a master Processes and multiple worker process .

master The process acts as the interface between the whole process group and the user , At the same time, monitor the process , management .

worker Process to restart the service 、 Smooth upgrade 、 Change log file 、 Functions such as real-time validation of configuration files .

worker Used to handle basic network events ,worker There is equality between , They compete to process requests from clients .

nginx The process model of is shown in the figure :

d197c75cfec1062a3bcb873e136fdbf5.png

Creating master Process time , First, build what needs to be monitored socket(listenfd)

And then from master In progress fork() More than one worker process , So each worker Processes can listen to user requests socket.

Generally speaking , When a link comes in , all worker Will be notified , But only one process can accept this connection request , Everything else failed , It's called Panic group phenomenon .

nginx Provides a accept_mutex( The mutex ), With this lock , At the same time , There will only be one process in accpet Connect , In this way, there won't be any question of surprise .

To open the first accept_mutex Options , Only got accept_mutex The process will be added accept event .

nginx Use a call ngx_accept_disabled To control whether to compete accept_mutex lock .

ngx_accept_disabled = nginx The total number of all connections for a single process / 8 - Number of free connections .

When ngx_accept_disabled Greater than 0 when , Not trying to get accept_mutex lock

ngx_accept_disable The bigger it is , The more opportunities you give up , The more opportunities other processes have to acquire locks . Don't go to accept, Every worker The number of connections to the process is under control , The connection pool of other processes will be utilized , such ,nginx It controls the balance of connections between multiple processes .

Every worker Processes have a separate connection pool , The size of the connection pool is worker_connections.

The connection pool here doesn't contain real connections , It's just a worker_connections One size ngx_connection_t Array of structures .

also nginx Through a linked list free_connections To save all the free ngx_connection_t.

Every time I get a connection , Get one from the free link list , After use , Put it back in the free link list .

One nginx The maximum number of connections that can be established , Should be worker_connections * worker_processes.

Of course , This is the maximum number of connections , about HTTP Ask local resources to say , The maximum number of concurrent that can be supported is worker_connections * worker_processes,

And if it is HTTP As a reverse agent , The maximum number of concurrent should be worker_connections * worker_processes / 2.

Because as a reverse proxy , Each concurrency establishes a connection to the client and back-end services , Will take up two connections .


Nginx Handle HTTP Request flow

http Requests are typical request - Response type Network protocol .http It's a document agreement , So we are analyzing request lines and request headers , And output response line and response header , It's often done line by line .

Usually after a connection is established , Read a row of data , Analyze the... Contained in the request line method、uri、http_version Information .

Then process the request header line by line , And on request method And the information of the request header to determine whether there is a request body and the length of the request body , Then read the request body .

After getting the request , We process requests to produce data that needs to be output , And then regenerate it into a response line 、 Response header and response body .

After sending the response to the client , A complete request is processed .

The whole processing flow chart is as follows :

504435de7a2865eeb54bb626b324e36d.png

Today's good article recommendation

GitHub It's very practical 40 Open source JAVA project

XShell It's too expensive ? Try open source NuShell, To use !

GET and POST What is the essential difference between requests ? After reading it, I feel too ignorant ...

MyBatis Bulk insert data you're still using foreach? Your server didn't crash ?

65c245ddc59fef66d498984d129812c7.png

I'm looking at one less bug 

copyright notice
author[androidstarjack],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2022/175/202206240725006421.html

Random recommended