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:22【androidstarjack】
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 :
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 :
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 :
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 !
MyBatis Bulk insert data you're still using foreach? Your server didn't crash ?
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
The sidebar is recommended
- How to read files using JavaScript
- Example of JavaScript FileReader API processing files
- Springboot+vue project tourism information recommendation system [source code open source]
- Ultra wideband pulse positioning scheme, UWB precise positioning technology, wireless indoor positioning application
- Elementui used tabs to step on the pit
- Ant Design Vue - remove the input component border and the border blue shadow effect when getting focus
- Get httponly protected cookies across sub domains through XSS
- Coding specification - Vue project
- Code writing specification - Javascript
- Code specification - HTML
guess what you like
Code writing specification - CSS
CSS Notes 6
Quickly set up PgSQL for serverless
404405 troubleshooting after Vue uses proxy packaging
Vue3 TS learning
What is the charm of python, which is warmly pursued by countless programmers?
HTML Basics
CSS Foundation
CSS (PC side) three methods of traditional page layout
Inline element, block element, inline block element
Random recommended
- 19、 Lua garbage collection of enterprise rapid development platform spring cloud+spring boot+mybatis+elementui
- Detailed introduction to render props and high-level component hoc in react tutorial
- Introduction to high order components (HOC) in react tutorial
- Front end and back end love and hate -- sequel
- When Vue adds on-demand loading to the UI library and starts the project, an error is reported in babel-preset-es2015
- Select in element uses typescript to define initial data, which has no effect
- Vue video player of Vue player plug-in
- React configuration component path reference @ to represent SRC root path
- Use less and CSS modules in react scaffold Engineering
- JavaScript plug-in download instructions and introduction of the web front end, NPM, install, save, and require
- Plug in download instructions and introduction of Vue in the web front end, NPM, install, save
- Summary of front-end common interview questions
- JavaScript automatically adds tag UTF-8 encoding
- Summary of three for loop statements in front-end JavaScript
- Vue hook function
- Monitor page rotation in Vue, and display in full screen on echarts mobile terminal
- Webpack learning notes series 07- how it works
- Webpack learning notes series 06- Packaging Optimization
- Webpack learning notes series 08-hmr hot update
- [Vue] understanding and thinking of new features based on Vue 3
- Fasthttp: go framework ten times faster than net/http (server)
- The difference between preload and prefetch and how to optimize in webpack projects
- Several ways of calling methods to transfer parameters in react render
- Axios usage
- LabVIEW finds prime numbers in an array of n elements
- Elementui form custom regular expression validation text box
- Export markdown to HTML, PDF with browser
- Experience summary of typescript transformation process of old vue2.x projects
- Front end development uses graphql -- vue3 uses graphql
- JS to get the last element of the array
- About Axios request - delete method
- The salon for the first anniversary of the open source of oasis, an ant interactive graphics engine, is coming. On February 26, we will see you in the live studio!
- Best practices for cookie notification
- How does HTML5 implement live streaming? It's worth learning!
- Record webpackdemo configuration
- Android studio uses iconfont Ali vector icon library
- HttpMediaTypeNotSupportedException: Content type ‘application. yml/json; charset=UTF-8‘ not supported
- CSS notes
- Nginx enables geoip2 module to realize national city identification - the road to dream
- Database to query the quantity of books lent in this month. If it is higher than 10, it will display "more than 10 books lent in this month". Otherwise, it will display "less than 10 books lent in this month"