What happens after entering the address in the browser
The browser is a tool for us to surf the Internet. We usually watch videos and check information. Internet surfing is inseparable from it, so do you know what the browser does after you hit the Enter key in the browser address bar? Today I will share with you the work behind the browser.
Step 1: Enter the domain name in the browser
For example, enter: www.php.cn
Step 2: The browser searches for the IP address of the domain name
The browser will parse the entered domain name into the corresponding IP. The process is as follows :
1. Search the browser cache: Because browsers generally cache DNS records for a period of time, different browsers may have different times, usually ranging from 2 to 30 minutes. The browser will search for these caches. If If there is a cache, return the IP directly, otherwise take the next step.
2. Search the system cache: After the IP is not found in the browser cache, the browser will make a system call (gethostbyname in Windows) to search for the hosts file of the local machine. If found, the IP will be returned directly, otherwise Next step.
3. Search the router cache: If the query in steps 1 and 2 fails, you need to use the network. The router generally has its own DNS cache. Send the previous request to the router to find the ISP service provider cache DNS. server, if the IP is found, it will be returned directly, if not, continue to search.
4. Recursive query: If the above steps cannot be found, the ISP's DNS server will perform a recursive query. The so-called recursive query means that if the local domain name server queried by the host does not know the IP address of the queried domain name , then the local domain name server will continue to send query request messages to other root domain name servers as a DNS client, instead of letting the host perform the next query on its own. (The local domain name server address is obtained through the DHPC protocol, and DHPC is responsible for allocating IP addresses)
5. Iterative query: The local domain name server uses iterative query, and it first queries a root domain name server. The local domain name server's query to the root domain name server generally uses iterative query. The so-called iterative query means that after the root domain name server receives the query request message sent by the local domain name server, it either tells the local domain name server which domain name server it should query next, and then the local domain name server performs subsequent queries on its own. (rather than replacing the local name server for subsequent queries).
Step 3: The browser establishes a TCP connection with the target server
1. After the host browser obtains the IP address of the target server through DNS resolution, it establishes a TCP connection with the server. TCP connection.
2.TCP 3-way handshake connection: the client where the browser is located sends a connection request message to the server (SYN flag is 1); after receiving the message, the server agrees to establish the connection and sends a confirmation message to the client (SYN and ACK flag bits are both 1); after the client receives the confirmation message, it sends a message to the server again to confirm that the confirmation message has been received; here the TCP connection between the client and the server is established and the process begins communication.
Step 4: The browser sends a request through the http protocol
The browser initiates an HTTP-GET method message request to the host. The request contains the accessed URL, that is, http://www.php.cn/, KeepAlive, long connection, as well as User-Agent user browser operating system information, encoding, etc. It is worth mentioning the Accept-Encoding and Cookies items. Accept-Encoding generally uses gzip to transmit html files after compression. If Cookies is accessed for the first time, the server will be prompted to establish user cache information. If not, you can use the corresponding key value of Cookies to find the corresponding cache. The cache stores the user name, password and some user settings.
Step 5: Some services will make permanent redirect responses
For large websites with multiple host sites, load balancing or importing traffic can improve SEO rankings , often not directly returning the requested page, but redirecting. The returned status code is not 200OK, but a redirection code starting with 301,302. After the browser obtains the redirection response, it finds the redirection address in the Location item in the response message, and the browser can access it again in the first step. .
The role of redirection: Redirection is for load balancing or importing traffic to improve SEO rankings. Using a front-end server to accept requests and then loading them on different hosts can greatly improve the site's concurrent business processing capabilities; redirection can also concentrate access from multiple domain names to one site; because baidu.com, www.baidu. com will be considered by search engines to be two websites, and the number of links to each will be reduced, thereby lowering the ranking. Permanent redirection will associate the two addresses, and search engines will consider them to be the same website, thus improving the ranking.
Step 6: The browser tracks the redirect address
When the browser knows the final access address after the redirection, it resends an http request and sends the content Same as above.
Step 7: The server processes the request
The server receives the get request, then processes and returns a response.
Step 8: The server sends an HTML response
Return status code 200 OK, indicating that the server can respond to the request and return the message, because the Content-type in the header For "text/html", the browser renders it as HTML instead of downloading the file.
Step 9: Release the TCP connection
1. The host where the browser is located sends a connection release message to the server, and then stops sending data;
2. After receiving the release message, the server sends a confirmation message, and then sends the untransmitted data on the server;
3. After the server data transmission is completed, it sends a connection release message to the client;
4. After receiving the message, the client sends a confirmation, and then waits for a period of time before releasing the TCP connection;
Step 10: The browser displays the page
When the browser has not fully accepted all the HTML documents, it has already started to display this page. The browser receives the returned data packet and renders the corresponding data according to the browser's rendering mechanism. The rendered data performs corresponding page rendering and footstep interaction.
Step 11: The browser sends and obtains other content embedded in HTML
For example, some style files, image URLs, js file URLs, etc., the browser will Re-send the request through these URLs. The request process is still a process similar to HTML reading, querying the domain name, sending the request, redirecting, etc. However, these static files can be cached in the browser. Sometimes these files are accessed directly from the cache without going through the server. Some websites also use third-party CDNs to host these static files.
The above is the detailed content of What happens when the browser enters the address. For more information, please follow other related articles on the PHP Chinese website!