A complete HTTP request process

Source: Internet
Author: User


Write in front

analyzing HTTP request processing can help readers understand the Web service architecture more deeply, and lay a good foundation for future promotion, the following process is my summary after studying lamp architecture, if there are errors, Please correct me. 650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M00/A7/39/wKioL1nkBtegqRdtAAD1yuQt-S8463.png "title=" 6943703-ea92391e79e7565f.png "alt=" Wkiol1nkbtegqrdtaad1yuqt-s8463.png "/>

Request Processing Process:

Domain Name resolution -- Establish a connection -- receive requests -- process requests -- access resources - build response Messages -- Send a response message -- logging

Domain Name resolution

Suppose the user enters in the browser address bar HTtp://yangzhiheng.blog.51cto.com initiates a request. First, the domain name (exactly known as the fully qualified domain name or hostname) is resolved to the IP address. So how to parse it?

Parse Order:

check browser itself cache ->  OS Span style= "background: #FFFFFF; font-size:16px;font-family: ' Courier New ';" >dns cache –> file –> parsing

  1. Check the browser's own DNS cache to see if it has the FQDN if the corresponding entry does not expire, it resolves to the corresponding IP address. If there is no entry or the entry has expired, then Step 2 is performed . View cache details in Google Chrome you can enter chrome://net-internals/#dns in the address bar to view

  2.  Check that the operating system itself corresponds to theDNSif the cache has a corresponding entry, forLinuxhost, system default is notDNSto cache. If the operating system'sDNSThe corresponding entry for the non-expired cache is found, and the resolution is resolved to this end. Otherwise, proceed to the next step. WindowsSystem View SystemDNSThe cache can be usedIpconfig/displaydnscommand.

  3.  Checkhostswhether there is a file in theFQDNThe corresponding entry, if any, is resolved successfully, and the next step is not performed. In theWindowsin the systemhostsfile is locatedC:\Windows\Sytem32\drivers\etc\directory. Linuxthe system is located in/etc/hostsdirectory.

  4. If an entry for the domain name does not exist in the Hosts file, the browser initiates a domain name resolution request to the local DNS server. The Domain name resolution request uses UDP port 53, and the local DNS request is a recursive request. The local DNS server first checks its own cache for entries that correspond to that FQDN. If there is a cut and no expiration is returned to the browser, parsing succeeds. If the corresponding entry is not found, the local DNS server initiates an iterative DNS resolution request, first to the root name server, the root domain server in the global 13. If the root domain server is storing the IP address of the DNS server that corresponds to the top-level domain name. In yangzhiheng.blog.51cto.com, for example, the root domain name is returned to the local DNS server for the DNS server address of a COM domain, the local DNS server requests the FQDN request from the COM domain name server, and the COM domain name server returns the DNS service for a 51cto domain to the local server The correct IP address for the local DNS, which is eventually returned to the The local server gets the IP address and caches it and returns it to the browser. The browser is accessed based on that IP address.

DNS the detailed parsing process, the reader can refer to http://vinsent.blog.51cto.com/13116656/1967876 this article to deepen understanding.


Establish a connection

Get IP address, the browser opens a random port to initiate a TCP link request to the Web server's port , and after 3 handshakes , a TCP connection is established , and then the browser initiates the httpd request.

To establish a connection process:

three-time handshake -- initiating an HTTP request

Three-time handshake process

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M01/08/89/wKiom1nkCW-C7CjrAADns2zTVnQ666.png "title=" 1.png "alt=" Wkiom1nkcw-c7cjraadns2ztvnq666.png "/>

  1.   assumes the client a tcp< Span style= "font-family: ' The song Body '; > client program), server b run httpd service program ( Tcp server program). The first two ends of the tcp processes are in a state. Client a B passive open connection (start httpd service, make 80

  2. server b after receiving the connection request message, if you agree with the client a< Span style= "font-family: ' The song Body '; > Establish the connection to the a send confirmation. In the confirmation message syn ack bit all 1 also choose an initial serial number for yourself seq=y b enter syn-rcvd has received the client a connection request, waiting for client A confirmation.

  3. the client process received B after confirmation, also to B 1, confirmation number ack=y+1,< Span style= "font-family: ' The song Body '; > I expect to receive the Y+1 tcp The agreement stipulates that Ack The confirmation packet can be carried, However, if you do not carry data, you do not consume serial numbers. That is, if the message is sent out, if you do not carry the data, the next message will be sent the serial number is still seq=x+1 tcp connect to establish. The client enters state. When the server b after receiving the confirmation message, also enter established state.

TCP Why do you have to shake three times?

in daily life, the establishment of a connection requires only two handshake should be able, the client sends a request, the server sends a confirmation message can be established connection. But why does client A have to send A confirmation message again? The main purpose is to prevent the failed connection request message segment from suddenly being transmitted to server B, resulting in an error.

A failed connection request message segment is generated, for example, client a sends a connection request message due to some network reasons stranded on a network node, client a mistakenly think that the packet is lost, and then send a connection request message, After the normal arrival of the message and the data transmission is completed, the message is transmitted to server B. The original one this is a long-overdue message, but B after receiving this message, it is mistaken that client A and send a connection request. Assuming that the three-time handshake is not used, server B confirms that the new connection is established, so in order to prevent the last occurrence, there are three handshakes to establish the connection.

To initiate an HTTP request:

After TCP establishes a connection, the client initiates an http request. The request message format is as follows

start line: Request method    requested protocol version

body

Both the request message and the response message are in accordance with the message format, where the request method generally has get,post,head,put,delete,trace,option, etc. . the URL is the same resource identifier that describes the location of a particular resource on the server, consisting of:scheme://server:port/path/to/resource. The protocol version tries to use the http/1.1 version. Header information is typically followed by a colon separating the host name to be requested.

Receiving requests

The job to receive a request is to receive a request from the network for a resource in a request message. The model that receives the request includes the following categories:

Single Process I/O model : initiates a process that processes user requests and processes only one at a time, and multiple requests are serially responded to.

Multi-Process I/O architecture: Multiple processes are started in parallel, and each process processes a request.

Multiplexing I/O structure: A process responds to n requests.

multithreaded Model : a process generates n threads, and each thread responds to a user request.

Event-driven: Event-driven .

Multithreading for Multiplexing I/O structure: Starts multiple m processes, each responding to N requests.

Processing requests

the request message is analyzed, and the requested resource and request method are obtained. in the case of Apache 's prefork mode of operation, its management process chooses a worker process to process the request after receiving the request message and obtains its request method. Information about the URL of the resource .

Access resources

in the case of request processing, it is generally necessary to access back-end resources, For example,the lamp architecture, the Apache worker process forwards the request to the PHP-FPM management process (the default port is 9000). the PHP-FPM management process assigns a worker process to process the index.php request, the worker process locates the index.php file in the server path , parses, compiles. Then execute the compiled PHP code, at this time generally also access to the backend database server and so on. Get the result of the request and return the result to the Apache server.

Building response Messages

Apache starts building a response message after getting the returned request result. The response message includes a response status code, a response header, and a response body if a response body is generated. at this point, there is a more important point is the resource access redirection problem.

Web The response built by the service is not a resource requested by the client, but a different access path for the resource: it can be divided into permanent redirection and temporary redirection.

Permanent redirection: curl-i http://www.360buy.com the results obtained http/1.1 301 Moved Permanently

Temporary redirection: curl-i http://www.taobao.com  the results obtained http/1.1 302 Found

Send Response message

after the response message is built, the response message is sent .

Record log

Finally, when the transaction ends, Web The server adds an entry in the log file to describe the transaction that was executed


This article is from the "Keep Simple Keep Stupid" blog, make sure to keep this source http://yangzhiheng.blog.51cto.com/11586378/1972634

A complete HTTP request process

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.