返回   青蛙论坛 » IT论坛 » 操作系统类
用户名
密码
帮助 会员 日历 标记论坛为已读

回复
 
主题工具 显示模式
旧 03-19-2008, 16:07   #1
chyangwa
蝌蚪-小毛驴
 
chyangwa的头像
 
注册日期: Nov 2000
来自: 上海
帖子: 8,285
精华: 6
HTTP Header about on LoadBalance and Reverse Proxy environment

HTTP Header about on LoadBalance and Reverse Proxy environment

http://enissue.com/archive/76/http-header-about-on-loadbalance-and-reverse-proxy-environment.html


http://atman.memoab.com/articles/92
Summary

在负载均衡环境[LVS, LoadBalance设备]中有关HTTP Header的注意事项和在反向代理中的相关项.

Matter

用户体验在用户输入你的网址和按下回车的刹那即显效果,速度是不可或缺的因素之一. 本文中不会涉及到网络带宽服务器架构或是css和javascripts的合并或分割. 只对工作中因负载均衡环境下影响和制约到包括网站页面本地缓存和过期时间的阐述, 我们知道多个HTTP请求和内容压缩将造成服务器CPU的上扬和进程/线程的消耗,但原则上只让用户与服务器群体传输尽量少的数据流,只传输更新的内容,且让静态数据贮存在用户本地.

在传输资料前,TCP做的SYN/ACK建立连接需要消耗大半的时间,


所以我们可在后半用并发连接和持续连接在提速,表现在Apache 的配置方式可在httpd.conf中找到答案:
# prefork MPM
# StartServers: number of server processes to start
# MinSpareServers: minimum number of server processes which are kept spare
# MaxSpareServers: maximum number of server processes which are kept spare
# MaxClients: maximum number of server processes allowed to start
# MaxRequestsPerChild: maximum number of requests a server process serves

# worker MPM
# StartServers: initial number of server processes to start
# MaxClients: maximum number of simultaneous client connections
# MinSpareThreads: minimum number of worker threads which are kept spare
# MaxSpareThreads: maximum number of worker threads which are kept spare
# ThreadsPerChild: constant number of worker threads in each server process
# MaxRequestsPerChild: maximum number of requests a server process serves

# KeepAlive: Whether or not to allow persistent connections (more than
# one request per connection). Set to “Off” to deactivate.
#
KeepAlive On|Off

多道处理模块(MPM)中的预先派生进程比worker的线程与进程混合模式要稳定,但接受的请求海量大. 可在安装Apache Web使用configure脚本时用–with-mpm=worker来选择worker或者默认使用Prefork的预先派生进程模式.

一个Worker MPM的样例:

#后一个为改善后的worker.c 的样式

持续连接在HTTP/1.0和HTTP 1.1中参数实现不同,而在RFC2616中建议一个连接对某服务器或反向代理最多两个持续连接,故避免在用户等待下载html时而Load 不出图片的方式是将图片交由另外的图片服务器来完成.

压缩带给数据传输的帮助是巨大的,在旧的Apache版本中多使用mod_gzip,现今在 Apache中已有mod_deflate代替. 浏览下概述:
概述

mod_deflate模块提供了DEFLATE输出过滤器,允许服务器在将输出内容发送到客户端以前进行压缩,以节约带宽。

大半用户使用的浏览器(IE,Firefox) 在使用HTTP Watch时你可查看到浏览器能接受的内容编码:

Accept-Encoding: gzip,deflate

我们的一个结果是让数据传输量少了3/4, 呵呵. 值得赞叹.

在你的配置文件中请注意提防那些“自以为是”的浏览器, 它们 不能很好的理解压缩数据.

# 插入过滤器
SetOutputFilter DEFLATE

# Netscape 4.x 有一些问题…
BrowserMatch ^Mozilla/4 gzip-only-text/html

# Netscape 4.06-4.08 有更多的问题
BrowserMatch ^Mozilla/4\.0[678] no-gzip

# MSIE 会伪装成 Netscape ,但是事实上它没有问题
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
# 不压缩图片
SetEnvIfNoCase Request_URI \
\.(?:gif|jpe?g|png)$ no-gzip dont-vary

# 确保代理不会发送错误的内容
Header append Vary User-Agent env=!dont-vary

但在使用如Squid等反向代理中时注意附加Vary header对代理服务器的影响

mod_deflate模块发送一个”Vary: Accept-Encoding“HTTP应答头以提醒代理服务器:只对发送了正确”Accept-Encoding“头的客户端发送缓存的应答。这样可以防止不能正确处理压缩内容的浏览器接受到经过压缩的内容。

如果你按照某些特殊的条件拒绝了某些客户端的访问(比如User-Agent头),你必须手动配置一个额外的Vary头提醒代理服务器做额外的限制。比如,在一个典型的配置中的某处,如果额外的DEFLATE过滤器是否生效取决于User-Agent头,你应当在此处添加:
Header append Vary User-Agent

如果依照除请求头以外的其他条件决定是否使用压缩(例如:HTTP版本),你必须设置Vary头的值为”*“来完全阻止代理服务器的缓存。
示例

Header set Vary *

在Squid 2.6中对经过压缩的数据能Cache, 来提高 缓存命中率.
# TAG: cache_vary
# Set to off to disable caching of Vary:in objects.
#
#Default:
# cache_vary on

即当你的前端代理服务器实行加速WEB行为需要确认Cache_vary为开状态.

我们需要作些改变来合理使用Apache Modules, 对于频繁变动的页面内容我们可以在程序设置header来不缓存. Cal Henderson充分利用了Http 1.1规范的Last-Modified和ETag 来Cache内容.
3.11 Entity Tags

Entity tags are used for comparing two or more entities from the same requested resource. HTTP/1.1 uses entity tags in the ETag (section 14.19), If-Match (section 14.24), If-None-Match (section 14.26), and If-Range (section 14.27) header fields. The definition of how they are used and compared as cache validators is in section 13.3.3. An entity tag consists of an opaque quoted string, possibly prefixed by a weakness indicator.

entity-tag = [ weak ] opaque-tag
weak = “W/”
opaque-tag = quoted-string

A “strong entity tag” MAY be shared by two entities of a resource only if they are equivalent by octet equality.

A “weak entity tag,” indicated by the “W/” prefix, MAY be shared by two entities of a resource only if the entities are equivalent and could be substituted for each other with no significant change in semantics. A weak entity tag can only be used for weak comparison.

An entity tag MUST be unique across all versions of all entities associated with a particular resource. A given entity tag value MAY be used for entities obtained by requests on different URIs. The use of the same entity tag value in conjunction with entities obtained by requests on different URIs does not imply the equivalence of those entities.

简单的说ETag Header是文件修改时间、文件大小和inode号生成的校验和(checksum),那在多台服务器的负载均衡环境下会因部署内容的时间差异造成ETag的不同,故虽然请求同一个数据但会因不同机器的ETag而影响了响应. 具体表现为用户在第一次请求某一内容时下载而再次时浏览器会发现ETag不同而再次请求下载. [使用HTTP Watch查看,再次刷新时查看是否响应码为:304]

那可有对策? 嘿嘿, 试用下万能的Apache 核心指令FileEtag吧 .
top
FileETag 指令
说明 用以创建ETag应答头的文件的属性
语法 FileETag component …
默认值 FileETag INode MTime Size
作用域 server config, virtual host, directory, .htaccess
覆盖项 FileInfo
状态 核心(C)
模块 core

FileETag指令配置了当文档是基于一个文件时用以创建ETag(实体标签)应答头的文件的属性(ETag的值用于进行缓冲管理以节约网络带宽)。在Apache1.3.22及以前,ETag的值总是由文件的inode(索引节点)、大小、最后修改时间决定。FileETag指令可以让您选择(如果您想进行选择)这其中哪些要素将被使用。主要关键字如下:

INode
文件的索引节点(inode)数
MTime
文件的最后修改日期及时间
Size
文件的字节数
All
所有存在的域,等价于:
FileETag INode MTime Size
None
如果一个文档是基于文件的,则不在应答中包含任何ETag头

可以在INode, MTime, Size前加上”+“或”-“以改变由上层继承下来的默认值。任何没有上述前缀的关键字将立刻完全取消继承下来的设置。

如果一个目录的配置包含了”FileETag INode MTime Size“而其一个子目录包含了”FileETag -INode“那么这个子目录的设置(并会被其下任何没有进行覆盖的子目录继承)将等价于”FileETag MTime Size“。

OK, 因我们的不同服务器上的文件大小是一样的, 我们可以让创建的ETag基于数据大小.

FileETag MTime Size

这里不得不提的另一个机制Last-Modified.

* 页面必须包含Last-Modified: 标记
一般纯静态页面本身都会有Last-Modified信息,动态页面需要通过函数强制加上,比如在PHP中:
// always modified now
header(”Last-Modified: ” . gmdate(”D, d M Y H:i:s”) . ” GMT”);

我们用Apache mod_expires对text和image类型文件加有效期,控制应答时的Expires头内容和Cache-Control头的max-age指令. 这样客户端浏览器从缓存请求数据而不是服务器端. 当缓存中数据失效或过期,才决定从服务器更新数据.

示例:

# 启用有效期控制
ExpiresActive On
# GIF有效期为1个月
ExpiresByType image/gif A2592000
# HTML文档的有效期是最后修改时刻后的一星期
ExpiresByType text/html M604800

动态数据的过期定义如PHP可在程序行中加入:

//一个月过期

header(”Expires: “.gmdate(”D, d M Y H:i:s”, time()+2592000).” GMT”);
header(”Cache-Control: max-age=2592000″);

Resource
Squid Vary Header Support
Squid ETag Header Support
Cal Henderson 《Serving JavaScript Fast》Jennifer Vesperman 《Cache-Friendly Web Pages》
HTTP Header 文档
chyangwa离线中   回复时引用此帖
回复


主题工具
显示模式

发帖规则
不可以发表新主题
不可以回复主题
不可以上传附件
不可以编辑您的帖子

vB 代码开启
[IMG]代码开启
HTML代码关闭
论坛跳转



所有的时间均为北京时间。 现在的时间是 06:08.


NewvBB Core 1.1 Final - vBulletin v3.0.3
中文化与插件制作 NewVBB.com™ 2024。
友情连接        
摩托车.上海.中国        
         
         
         
         
         

上海市通信管理局
沪ICP备010502
沪ICP备05000578号