When you enter a URL, what exactly happens in the background_HTML/Xhtml_Web page production

WBOY
Release: 2016-05-16 16:40:45
Original
1513 people have browsed it

As a software developer, you must have a complete hierarchical understanding of how web applications work. This also includes the technologies used by these applications: browsers, HTTP, HTML, web servers, requirements Processing and so on.

This article will take a deeper look at what happens in the background when you enter a URL~

1. First, you have to enter the URL in the browser :

2. The browser searches for the IP address of the domain name

When you enter a URL, what exactly happens in the background_HTML/Xhtml_Web page production

The first step in navigation is to find out the IP address of the visited domain name. The DNS lookup process is as follows:

Browser Cache – Browsers cache DNS records for a period of time. Interestingly, the operating system does not tell the browser how long to store DNS records, so different browsers will store a fixed time (ranging from 2 minutes to 30 minutes). System cache – If the required record is not found in the browser cache, the browser will make a system call (gethostbyname in Windows). This will obtain the record in the system cache. Router cache – Next, the previous query request is sent to the router, which usually has its own DNS cache. ISP DNS cache – The next thing to check is the server where the ISP caches DNS. The corresponding cache records can generally be found here. Recursive Search – Your ISP’s DNS servers perform a recursive search starting with the nameservers, from the .com top-level nameservers to Facebook’s nameservers. Generally, the cache of the DNS server will contain the domain name in the .com domain name server, so the matching process to the top-level server is not necessary.

DNS recursive search is shown in the figure below:

When you enter a URL, what exactly happens in the background_HTML/Xhtml_Web page production

One thing that is worrisome about DNS is that an entire domain name like wikipedia.org or facebook.com only appears to correspond to a single IP address. Fortunately, there are several ways to eliminate this bottleneck:

Cycle DNS is the solution when multiple IPs are returned during DNS lookup. For example, Facebook.com actually corresponds to four IP addresses. A load balancer is a hardware device that listens on a specific IP address and forwards network requests to clustered servers. Some large sites generally use this expensive, high-performance load balancer. GeographyDNS improves scalability by mapping domain names to multiple different IP addresses based on the user's geographical location. This way different servers can't update the sync state, but it's great for mapping static content. Anycast is a routing technology that maps one IP address to multiple physical hosts. The fly in the ointment is that Anycast does not adapt well to the TCP protocol, so it is rarely used in those solutions.

Most DNS servers use Anycast for efficient low-latency DNS lookups.

3. The browser sends an HTTP request to the web server

When you enter a URL, what exactly happens in the background_HTML/Xhtml_Web page production

Because dynamic pages like Facebook homepage will expire quickly or even immediately in the browser cache after opening, there is no doubt that they cannot be read from it.

So, the browser will send the following request to the server where Facebook is located:

GET http://facebook.com/ HTTP/1.1<br> Accept: application/x-ms-application, When you enter a URL, what exactly happens in the background_HTML/Xhtml_Web page production/jpeg, application/xaml+xml, [...]<br> User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; [...]<br> Accept-Encoding: gzip, deflate<br> Connection: Keep-Alive<br> Host: facebook.com<br> Cookie: datr=1265876274-[...]; locale=en_US; lsd=WW[...]; c_user=2101[...]
Copy after login

GET This request defines the URL to be read: "http://facebook.com/". The browser defines itself (User-Agent header), and what type of response it wants to accept (Accept andAccept-Encoding header). Connection The header asks the server not to close the TCP connection for subsequent requests.

The

request also contains the cookies stored by the browser for that domain name. As you may already know, cookies are keys that track the status of a website between page requests. In this way, cookies will store the login user name, the password assigned by the server and some user settings. Cookies are stored in the client computer as text files and sent to the server with each request.

There are many tools used to view original HTTP requests and their corresponding ones. The author prefers to use fiddler, but of course there are other tools like FireBug. These software will help a lot when optimizing your website.

除了获取请求,还有一种是发送请求,它常在提交表单用到。发送请求通过URL传递其参数(e.g.: http://robozzle.com/puzzle.aspx?id=85)。发送请求在请求正文头之后发送其参数。

像“http://facebook.com/”中的斜杠是至关重要的。这种情况下,浏览器能安全的添加斜杠。而像“http: //example.com/folderOrFile”这样的地址,因为浏览器不清楚folderOrFile到底是文件夹还是文件,所以不能自动添加 斜杠。这时,浏览器就不加斜杠直接访问地址,服务器会响应一个重定向,结果造成一次不必要的握手。 

4. facebook服务的永久重定向响应

When you enter a URL, what exactly happens in the background_HTML/Xhtml_Web page production

图中所示为Facebook服务器发回给浏览器的响应:

HTTP/1.1 301 Moved Permanently<br> Cache-Control: private, no-store, no-cache, must-revalidate, post-check=0,<br> pre-check=0<br> Expires: Sat, 01 Jan 2000 00:00:00 GMT<br> Location: http://www.facebook.com/<br> P3P: CP="DSP LAW"<br> Pragma: no-cache<br> Set-Cookie: made_write_conn=deleted; expires=Thu, 12-Feb-2009 05:09:50 GMT;<br> path=/; domain=.facebook.com; httponly<br> Content-Type: text/html; charset=utf-8<br> X-Cnection: close<br> Date: Fri, 12 Feb 2010 05:09:51 GMT<br> Content-Length: 0
Copy after login

服务器给浏览器响应一个301永久重定向响应,这样浏览器就会访问“http://www.facebook.com/” 而非“http://facebook.com/”。

为什么服务器一定要重定向而不是直接发会用户想看的网页内容呢?这个问题有好多有意思的答案。

其中一个原因跟搜索引擎排名有 关。你看,如果一个页面有两个地址,就像http://www.igoro.com/ 和http://igoro.com/,搜索引擎会认为它们是两个网站,结果造成每一个的搜索链接都减少从而降低排名。而搜索引擎知道301永久重定向是 什么意思,这样就会把访问带www的和不带www的地址归到同一个网站排名下。

还有一个是用不同的地址会造成缓存友好性变差。当一个页面有好几个名字时,它可能会在缓存里出现好几次。

5. 浏览器跟踪重定向地址

When you enter a URL, what exactly happens in the background_HTML/Xhtml_Web page production

现在,浏览器知道了“http://www.facebook.com/”才是要访问的正确地址,所以它会发送另一个获取请求:

GET http://www.facebook.com/ HTTP/1.1<br> Accept: application/x-ms-application, When you enter a URL, what exactly happens in the background_HTML/Xhtml_Web page production/jpeg, application/xaml+xml, [...]<br> Accept-Language: en-US<br> User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; [...]<br> Accept-Encoding: gzip, deflate<br> Connection: Keep-Alive<br> Cookie: lsd=XW[...]; c_user=21[...]; x-referer=[...]<br> Host: www.facebook.com
Copy after login

头信息以之前请求中的意义相同。

6. 服务器“处理”请求

When you enter a URL, what exactly happens in the background_HTML/Xhtml_Web page production

服务器接收到获取请求,然后处理并返回一个响应。

这表面上看起来是一个顺向的任务,但其实这中间发生了很多有意思的东西- 就像作者博客这样简单的网站,何况像facebook那样访问量大的网站呢!

Web 服务器软件
web服务器软件(像IIS和阿帕奇)接收到HTTP请求,然后确定执行什么请求处理来处理它。请求处理就是一个能够读懂请求并且能生成HTML来进行响应的程序(像ASP.NET,PHP,RUBY...)。

举 个最简单的例子,需求处理可以以映射网站地址结构的文件层次存储。像http://example.com/folder1/page1.aspx这个地 址会映射/httpdocs/folder1/page1.aspx这个文件。web服务器软件可以设置成为地址人工的对应请求处理,这样 page1.aspx的发布地址就可以是http://example.com/folder1/page1。

请求处理
请求处理阅读请求及它的参数和cookies。它会读取也可能更新一些数据,并讲数据存储在服务器上。然后,需求处理会生成一个HTML响应。

所 有动态网站都面临一个有意思的难点 -如何存储数据。小网站一半都会有一个SQL数据库来存储数据,存储大量数据和/或访问量大的网站不得不找一些办法把数据库分配到多台机器上。解决方案 有:sharding (基于主键值讲数据表分散到多个数据库中),复制,利用弱语义一致性的简化数据库。

委 托工作给批处理是一个廉价保持数据更新的技术。举例来讲,Fackbook得及时更新新闻feed,但数据支持下的“你可能认识的人”功能只需要每晚更新 (作者猜测是这样的,改功能如何完善不得而知)。批处理作业更新会导致一些不太重要的数据陈旧,但能使数据更新耕作更快更简洁。

7. 服务器发回一个HTML响应

When you enter a URL, what exactly happens in the background_HTML/Xhtml_Web page production

图中为服务器生成并返回的响应:

HTTP/1.1 200 OK<br> Cache-Control: private, no-store, no-cache, must-revalidate, post-check=0,<br> pre-check=0<br> Expires: Sat, 01 Jan 2000 00:00:00 GMT<br> P3P: CP="DSP LAW"<br> Pragma: no-cache<br> Content-Encoding: gzip<br> Content-Type: text/html; charset=utf-8<br> X-Cnection: close<br> Transfer-Encoding: chunked<br> Date: Fri, 12 Feb 2010 09:05:55 GMT<br> <br> 2b3Tn@[...]
Copy after login

整个响应大小为35kB,其中大部分在整理后以blob类型传输。

内容编码头告诉浏览器整个响应体用gzip算法进行压缩。解压blob块后,你可以看到如下期望的HTML:

br /> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><br> 
Copy after login
lang="en" id="facebook" class=" no_js">



...

关于压缩,头信息说明了是否缓存这个页面,如果缓存的话如何去做,有什么cookies要去设置(前面这个响应里没有这点)和隐私信息等等。

请注意报头中把Content-type设置为“text/html”。报头让浏览器将该响应内容以HTML形式呈现,而不是以文件形式下载它。浏览器会根据报头信息决定如何解释该响应,不过同时也会考虑像URL扩展内容等其他因素。

8. 浏览器开始显示HTML

在浏览器没有完整接受全部HTML文档时,它就已经开始显示这个页面了:

When you enter a URL, what exactly happens in the background_HTML/Xhtml_Web page production

9. 浏览器发送获取嵌入在HTML中的对象

When you enter a URL, what exactly happens in the background_HTML/Xhtml_Web page production

在浏览器显示HTML时,它会注意到需要获取其他地址内容的标签。这时,浏览器会发送一个获取请求来重新获得这些文件。

下面是几个我们访问facebook.com时需要重获取的几个URL:

图片
http://static.ak.fbcdn.net/rsrc.php/z12E0/hash/8q2anwu7.gif
http://static.ak.fbcdn.net/rsrc.php/zBS5C/hash/7hwy7at6.gif
CSS 式样表
http://static.ak.fbcdn.net/rsrc.php/z448Z/hash/2plh8s4n.css
http://static.ak.fbcdn.net/rsrc.php/zANE1/hash/cvtutcee.css
JavaScript 文件
http://static.ak.fbcdn.net/rsrc.php/zEMOA/hash/c8yzb6ub.js
http://static.ak.fbcdn.net/rsrc.php/z6R9L/hash/cq2lgbs8.js

这些地址都要经历一个和HTML读取类似的过程。所以浏览器会在DNS中查找这些域名,发送请求,重定向等等...

但 不像动态页面那样,静态文件会允许浏览器对其进行缓存。有的文件可能会不需要与服务器通讯,而从缓存中直接读取。服务器的响应中包含了静态文件保存的期限 信息,所以浏览器知道要把它们缓存多长时间。还有,每个响应都可能包含像版本号一样工作的ETag头(被请求变量的实体值),如果浏览器观察到文件的版本 ETag信息已经存在,就马上停止这个文件的传输。

试着猜猜看“fbcdn.net”在地址中代表什么?聪明的答案是"Facebook内容分发网络"。Facebook利用内容分发网络(CDN)分发像图片,CSS表和JavaScript文件这些静态文件。所以,这些文件会在全球很多CDN的数据中心中留下备份。

静态内容往往代表站点的带宽大小,也能通过CDN轻松的复制。通常网站会使用第三方的CDN。例如,Facebook的静态文件由最大的CDN提供商Akamai来托管。

举例来讲,当你试着ping static.ak.fbcdn.net的时候,可能会从某个akamai.net服务器上获得响应。有意思的是,当你同样再ping一次的时候,响应的服务器可能就不一样,这说明幕后的负载平衡开始起作用了。

10. The browser sends an asynchronous (AJAX) request

When you enter a URL, what exactly happens in the background_HTML/Xhtml_Web page production

Under the guidance of the great spirit of Web 2.0, the client still maintains contact with the server after the page is displayed.

Take the Facebook chat function as an example. It will continuously keep in contact with the server to update the status of your bright and gray friends in a timely manner. In order to update the status of these friends whose avatars are lit, the JavaScript code executed in the browser will send an asynchronous request to the server. This asynchronous request is sent to a specific address and is a programmatically constructed get or send request. Still in the Facebook example, the client sends a publish request to http://www.facebook.com/ajax/chat/buddy_list.php to obtain the online status information of which of your friends is online.

When mentioning this pattern, we must talk about "AJAX"--"Asynchronous JavaScript and XML", although there is no clear reason why the server responds in XML format. For another example, for asynchronous requests, Facebook will return some JavaScript code snippets.

Among other things, fiddler is a tool that allows you to see the asynchronous requests sent by the browser. In fact, you can not only passively watch these requests, but also proactively modify and resend them. AJAX requests are so easy to fool, which really makes those online game developers who keep score very depressed. (Of course, don’t lie to people like that~)

The Facebook chat function provides an interesting problem case regarding AJAX: pushing data from the server to the client. Because HTTP is a request-response protocol, the chat server cannot send new messages to the client. Instead, the client has to poll the server every few seconds to see if it has any new messages.

Long polling is an interesting technique to reduce server load when these situations occur. If the server has no new messages when polled, it ignores the client. When a new message from the client is received before the timeout has expired, the server will find the unfinished request and return the new message to the client as a response.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!