关于 Cookie-free Domains(为什么将静态图片,js,css存放到单独的域名?)
来源:互联网 发布:网络营销数据分析 编辑:程序博客网 时间:2024/05/17 23:12
这篇文章对高性能web开发具有参考性:http://developer.yahoo.com/performance/rules.html
本文主要描述使用裸域名做网站主域名时,如何用子域名做 cookie-free domains。
看站点优化的文章,很多建议将站点的静态文件(如图片、js、css 等)放在一个专门的域名下访问,由于该域名与主站域名不同,所以浏览器就不会把主域名下的 Cookie 传给该域,减少了网络开销,一定程度提高了页面加载速度,特别是细碎静态文件特别多的情况下效果显著。大概这个域就叫着 cookie-free domain 吧。
Yahoo yslow 可以对网页性能进行评级,其中一个指标就是 “Use cookie-free domains”,描述如下:
When the browser requests a static image and sends cookies with the request, the server ignores the cookies. These cookies are unnecessary network traffic. To workaround this problem, make sure that static components are requested with cookie-free requests by creating a subdomain and hosting them there.大意是当浏览器请求一个静态图片并发送 Cookie 时,服务器会忽略 Cookie。Cookie 根本就没必要通过网络传输。为了解决这个问题,应当创建一个子域并将静态内存放在上面,以确保静态内容都是通过 cookie-free 的方式访问的。
有文章指出,如果主域名使用裸域名(根域名,例如 opoo.org)而静态内存使用子域名(例如 static.opoo.org)是无法达到 cookie-free 的。因为裸域的 cookie 会被传递给子域。即如果在 opoo.org 设置了cookie,那么访问子域 static.opoo.org 浏览器也会发送这个 Cookie,根本达不到 cookie-free 的目的。----笔者则认为这种说法不完全正确。
Cookie 有两个重要的属性:domain 和 path。当浏览器访问一个网站时,只有 domain 和 path 都满足条件时,才会发送相应的 Cookie 到这个网站的服务器。而服务器在要求浏览器设置 Cookie 时,也只能设置与当前网站匹配的 domain 属性。
Cookie domain 属性的匹配方法,简单示例如下:
- cookie.domain = "opoo.org" 该 Cookie 只能发给域 opoo.org,不能发到子域 www.opoo.org, static.opoo.org 等
- cookie.domain = ".opoo.org" 该 Cookie 可以发给域 opoo.org 及其下所有子域,包括 www.opoo.org, static.opoo.org 等
- cookie.domain = "www.opoo.org" 该 Cookie 只能发给域 www.opoo.org, 不能发给域 opoo.org 及 static.opoo.org 等
关于这个“点”,RFC 6265 和 RFC 2109 描述不完全一致,有兴趣的读者可以翻看原文。
RFC 6265 4.1.2.3 章节
4.1.2.3. The Domain Attribute The Domain attribute specifies those hosts to which the cookie will be sent. For example, if the value of the Domain attribute is "example.com", the user agent will include the cookie in the Cookie header when making HTTP requests to example.com, www.example.com, and www.corp.example.com. (Note that a leading %x2E ("."), if present, is ignored even though that character is not permitted, but a trailing %x2E ("."), if present, will cause the user agent to ignore the attribute.) If the server omits the Domain attribute, the user agent will return the cookie only to the origin server. The user agent will reject cookies unless the Domain attribute specifies a scope for the cookie that would include the origin server. For example, the user agent will accept a cookie with a Domain attribute of "example.com" or of "foo.example.com" from foo.example.com, but the user agent will not accept a cookie with a Domain attribute of "bar.example.com" or of "baz.foo.example.com".看这意思难道是说有没有前缀“点”,Cookie 都会传给子域?没太读懂。
但实际测试的效果跟我前面描述是一致的,即不带“点”的只能传给本域,不能传给子域。
读者可以自行检验:用 Chrome 开发人员工具或者 Firebug 观察 Github.com 和SourceForge.net 网站及其子网站的请求/响应头信息中的 Cookie 信息。这两个网站就是伺服在裸域名上的。
关于网站是否使用裸域,个人觉得,.com 的域名要 www 比较好,还可以利用浏览器快捷键;而 .org, .net 等域名用裸域挺不错的,简洁方便。
在基本搞清楚 cookie-free domains,笔者将本站域名换回到裸域,并设置了通过子域名访问图片等静态内容。
WordPress
WordPress 设置的 Cookie domain 本来就是以不带点的域名开头的,不需要额外设置。如果伺服在 www.opoo.org 则要修改 wp-config.php 设置 COOKIE_DOMAIN 属性。
Google 分析
在裸域名使用 Google 分析,需要在 js 代码中设置 cookieDomain 属性为 none,否则生成的 Cookie 域将带前缀点。
_gaq.push([
'_setDomainName'
,
'none'
]);
很不幸,暂时没有发现百度统计代码中有设置 cookieDomain 的地方,其生成的 Cookie 是带前缀点的,会引起 Cookie 污染,所以笔者直接删除了百度统计代码。
又注:
避免域名污染。 当浏览器向服务器请求一个静态资源时,会先发送同域名下的 cookie,服务器对于这些 cookie 不会做任何处理。因此它们只是在毫无意义的消耗带宽。所以你应该确保对于静态内容的请求是无coockie的请求。
如果给 http://126.com 设置了cookie,那么会感染所有子域名, 请求 http://www.126.com/logo.gif或者http://image.126.com/logo.gif 时便会带上讨厌的cookie。
所以要用单独的域名,以减少请求,提高网页性能。
来自:http://opoo.org/cookie-free-domains/
- 关于 Cookie-free Domains(为什么将静态图片,js,css存放到单独的域名?)
- Cookie-free Domains(为什么将静态图片,js,css存放到单独的域名?)
- 使用cookie-free domains的方法优化网站性能
- Cookie-free Domains 极限页面性能优化
- 免费域名注册-(Free domains for all)
- java实现将js、css、图片合并到html
- 关于Django工程应用mod_python部署到Apache上,css、js等静态文件的设置
- MySQL将表数据单独存放文件
- 将样式信息移动到单独的CSS文件中/-----------/CSS的局限性和使用服务器端代码设置样式
- 单独存放文件的script
- 实现将一个存放图片的集合赋值到Photoview中,并用Viewpager展示出来
- COOKIE的JS静态类
- 为什么需要把应用中出现的文字单独存放在string.xml文件中呢?
- 为什么需要把应用中出现的文字单独存放在string.xml文件中
- 如何访问到静态的文件,如jpg,js,css?
- 关于mysql数据库图片存放的问题
- 关于cookie存放数据问题
- 将tomcat映射到不同的域名
- 黑马程序员_overload和override的区别
- 方法拦截器(MethodInterceptor)使用
- 公交吹响709路车辆专项整修集结号
- 国内各大互联网公司相关技术站点2.0版 (集合腾讯、阿里、百度、搜狐、新浪、360等共49个)
- 51单片机延时函数
- 关于 Cookie-free Domains(为什么将静态图片,js,css存放到单独的域名?)
- 单例模式
- RSA-生成密钥
- [Android] Android开发优化之——对Bitmap的内存优化
- logcat 显示指定应用程序信息
- HNOI2002 - 营业额统计
- android usb host 模式下开发 usb 口被device 占用如何连接PC查看logcat
- delphi2009 用idhttp获取网页内容中文乱码
- [Android] Android开发优化之——使用软引用和弱引用