Http service details (1)-a complete http service request processing process, detailed request
To be familiar with a service, first of all, you need to understand the service process. This article explains in detail the http service request processing process.
A complete http request processing process
(1) Flowchart
(2) process details
0. DNS domain name resolution: recursive query and iterative Query
Recursive query: the client queries the first server and returns the final result.
Iterative query: The first server queries the root
1,
Establish a connection:Receiving or rejecting connection requests: three-way handshake
Improve HTTP connection performance:
Parallel Connection: Initiate concurrent HTTP requests through multiple TCP connections
Persistent connection: Keep-alive, persistent connection, reuse of TCP connection, to eliminate the delay of connection and close, determine whether to close the connection based on the number and time of transactions
Pipeline Connection: Initiate concurrent HTTP requests through a shared TCP Connection
Multiplexing connections: Alternate transmission of request and response packets (experimental phase, not yet implemented)
① Serial connection
② Parallel Connection
③ Persistent connection
④ Pipeline Connection
2,
Receive request: The process of receiving a request to a resource in the client request message,
Request Message
Web Access response model (Web I/O)
Single process I/O model:Start a processProcess user requests, and only one request is processed at a time.Serial response, Too old
Multi-process I/O model: ParallelStart Multiple processes,EachProcess ResponseOneConnection Request
Reuse the I/O structure: StartOne Process, At the same timeResponse to N connection requests,Connection Pool
Implementation Method: multithreading model and event-driven
Multithreading Model: A process generates N threads and each thread responds to a connection request.
Event-driven: One process processes N requests, Nginx
Processes: such as copying jobs, project groups, and resource consumption
Thread: such as human, lightweight
A process must have one thread. A process can have multiple threads.
Multiplexing multi-process I/O model: Start M processes. Each process responds to N connection requests and receives M * N requests at the same time.
3. Process requests
The server parses request packets and obtains the requested resources, request methods, and other information,HeaderAnd the optional entity to process the request
Metadata:Request Message Header
<Method> <URL> <VERSION>
HEADERS format name: value
<Request body>
Example:
Host: Host name requested by www.along.com
Server: Apache/2.4.7
Common HTTP request methods:
GET, POST, HEAD, PUT, DELETE, TRACE, OPTIONS
4,
Access resources:
The server obtains the requested resource web server in the request message, that is, the server that stores the web resource. It is responsible for providing the static Resources requested by the other party to the requester, or resources generated after dynamic operation.
The resource is placed in the specific path of the local file system: the root of the DocRoot Service
DocRoot --->/var/www/html
Example:/var/www/html/images/logo.jpg
Http://www.along.com/images/logo.jpg
Web server resource path ing method: Detailed description in the next section
(A) docroot
(B) alias
(C) virtual host docroot
(D) user's home directory docroot
5,
Construct response packets:
Once the Web server identifies the resource, it executes the action described in the request method and returns the Response Message. The response message contains the response status code and Response Header. If a response body is generated, the response body is also included.
1) response entity: if the transaction processing produces a response body, the content will be sent back in the Response Message. Response packets usually include:
Describes the response body.MIMETypeContent-TypeHeader
Describes the length of the response bodyContent-Length
Body content of the actual message
2) URL redirection: The response built by the web service is not the resource requested by the client, but another access path of the resource.
Permanent redirection: http://www.360buy.com ---> http://www.jd.com
Temporary redirection: http://www.taobao.com ---> https://www.taobao.com
3)MIME Type: Multimedia Mail Extension
The Web server is responsible for determining the MIME type of the response body. There are many ways to configure servers to manage MIME types and resources
Magic classification (SCANHeader information): The Apache web server can scan the content of each resource and associate it with a knownSchema table, Header(Called a magic file) to determine the MIME type of each file. This may be slow but convenient, especially when the file does not have a standard extension.
Explicit classification: You can configure the Web server so that it does not consider the file extension or content,Force specificThe file or directory content has a MIME type, such as php, which is not recognized by Apache and is forcibly recognized.
Type negotiation: some Web servers are configured to store resources in multiple document formats. In this case, you can configure the Web server so that it can determine the format (and related MIME types) used by negotiation with the user "best"
6,
Send Response Message
When the Web server sends data through a connection, it also faces the same problem as receiving data. The server may have many connections to various clients, some of which are idle, some are sending data to the server, and some are sending response data back to the client. The server must record the connection status and pay special attention to the handling of persistent connections. For non-persistent connections, the server should close the connection at its own end after sending the entire packet. For persistent connections, the connection may remain open. In this case, the server must correctly calculate the Content-Length header, otherwise the client will not be able to know when the response will end.
7,
Record logs
Finally, when the transaction ends, the Web Server adds an entry to the log file to describe the executed transaction.
Log Type:The next article will explain the meaning of log formats
AccessLog: now more and more important, big data Era
ErrorLog: troubleshooting
A separate http service communication process is provided:
The next article is more exciting ~