从输入网址到浏览器显示页面的详细过程:
browser checks cache; if requested object is in cache and is fresh, skip to #9
browser asks OS for server's IP address
OS makes a DNS lookup and replies the IP address to the browser
browser opens a TCP connection to server (this step is much more complex with HTTPS)
browser sends the HTTP request through TCP connection
browser receives HTTP response and may close the TCP connection, or reuse it for another request
browser checks if the response is a redirect (3xx result status codes), authorization request (401), error (4xx and 5xx), etc.; these are handled differently from normal responses (2xx)
if cacheable, response is stored in cache
browser decodes response (e.g. if it's gzipped)
browser determines what to do with response (e.g. is it a HTML page, is it an image, is it a sound clip?)
browser renders response, or offers a download dialog for unrecognized types
来源:http://stackoverflow.com/questions/2092527/what-happens-when-you-type-in-a-url-in-browser
下面是一些比较详尽的解释:
英文: http://igoro.com/archive/what-really-happens-when-you-navigate-to-a-url/
中文:http://www.cnblogs.com/wenanry/archive/2010/02/25/1673368.html
1、在浏览器中输入地址,按下回车键
2、浏览器查找域名的IP地址,dns按照先后顺序查找ip地址
浏览器缓存:浏览器会缓存DNS记录一段时间,不同浏览器会储存不同的固定的一个时间
系统缓存:如果在浏览器缓存里没有找到需要的记录,浏览器会做一个系统调用,获得系统缓存中的记录
路由器缓存
ISP DNS 缓存
递归搜索:ISP的DNS服务器从跟域名服务器开始进行递归搜索
3、浏览器给web服务器发送一个HTTP请求
HTTP客户机在端口80发起一个到服务器的TCP连接(三次握手),客户机通过建立的HTTP连接创建的套接字向服务器发送HTTP请求
GET http://facebook.com/ HTTP/1.1
Accept: application/x-ms-application, image/jpeg, application/xaml+xml, [...]
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; [...]
Accept-Encoding: gzip, deflate
Connection: Keep-Alive
Host: facebook.com
Cookie: datr=1265876274-[...]; locale=en_US; lsd=WW[...]; c_user=2101[...]
GET 这个请求定义了要读取的URL: “http://facebook.com/”。 浏览器自身定义 (User-Agent 头), 和它希望接受什么类型的相应 (Accept and Accept-Encoding 头). Connection头要求服务器为了后边的请求不要关闭TCP连接。请求中也包含浏览器存储的该域名的cookies。可能你已经知道,在不同页面请求当中,cookies是与跟踪一个网站状态相匹配的键值。这样cookies会存储登录用户名,服务器分配的密码和一些用户设置等。Cookies会以文本文档形式存储在客户机里,每次请求时发送给服务器。
4. facebook服务的永久重定向响应
HTTP服务器进程通过与请求连接相关的套接字接受请求报文,从存储器检索出对象,在一个HTTP响应报文中封装对象,并通过连接套接字向客户机发送响应报文。
HTTP/1.1 301 Moved Permanently
Cache-Control: private, no-store, no-cache, must-revalidate, post-check=0,
pre-check=0
Expires: Sat, 01 Jan 2000 00:00:00 GMT
Location: http://www.facebook.com/
P3P: CP="DSP LAW"
Pragma: no-cache
Set-Cookie: made_write_conn=deleted; expires=Thu, 12-Feb-2009 05:09:50 GMT;
path=/; domain=.facebook.com; httponly
Content-Type: text/html; charset=utf-8
X-Cnection: close
Date: Fri, 12 Feb 2010 05:09:51 GMT
Content-Length: 0
服务器给浏览器响应一个301永久重定向响应,这样浏览器就会访问“http://www.facebook.com/” 而非“http://facebook.com/”