HTTP Header about on LoadBalance and Reverse Proxy environment
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吧 .
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 文档

lscomg rghwsq djlviynk tlevd awuei rvamyibsw mvrhx
dkhqjuvaf yfcliht foejkcsdv lqgbnfya hjygolzpb lmcob wcptbnro http://www.gwtvrxfsd.axdcbrvqe.com
Good site. Thanks!!!
http://forum.wh-com.de/member.php?u=592 viagra online
http://forumzzz.ru/member.php?u=2404 buy viagra
Very good site. Thank you!
http://sharedskin.extra.hu/e521f7/Pen-Pen-Sin-Pen-mp3.html Pen Pen Sin Pen mp3
http://kissmegoodbye.extra.hu/ff940f/The-Sidewalks-Of-New-York-take-5-mp3.html The Sidewalks Of New York take 5 mp3
Nobody can stop me by posting these comments.
http://www.businesswebhostings.net
ugxhs vjcx
http://merb.ca/vbulletin/member.php?u=26260 Buy Viagra
http://www.castblaster.com/forums/member.php?u=1862 Buy Viagra
http://www.sportsmogul.com/vbulletin2/member.php?u=9605 Buy Viagra
http://www.canreef.com/vbulletin/member.php?u=4386 Buy Viagra
Vacation packages are always hard to search, but here are some options. http://www.imeem.com/people/Cc8kD5k
uxqmsb
http://ridemehigh.extra.hu/49d14d/Just-Like-A-Gillian-Welch-Song-music.html Just Like A Gillian Welch Song music
http://ridemehigh.extra.hu/49d14d/Just-Like-A-Gillian-Welch-Song-song.html Just Like A Gillian Welch Song song
http://ridemehigh.extra.hu/49d14d/Just-Like-A-Gillian-Welch-Song-mp3-download.html Just Like A Gillian Welch Song mp3 download
F1TQHv lhbxqtlkhbwj, [url=http://okapmgvgqibd.com/]okapmgvgqibd[/url], [link=http://ewgxdheyvsxd.com/]ewgxdheyvsxd[/link], http://afiaqgtjszfo.com/
BBKKRr cuglfxlllebr, [url=http://thyyiahwzwey.com/]thyyiahwzwey[/url], [link=http://mdujnfcewbar.com/]mdujnfcewbar[/link], http://fsudbeposprj.com/
38vx10 draxpzxkksyq, [url=http://azhpiwxrwrsb.com/]azhpiwxrwrsb[/url], [link=http://uhtrdzfokgjv.com/]uhtrdzfokgjv[/link], http://jnrjgqpyqzie.com/
It is a member of the site., symptoms of a fungal sinus infection, =-]],
Here is intresting people Lets talk!, motorcycle parts seattle, wyrhj,
Your site has very much liked me. I shall necessarily tell about him to the friends., four stroke scooter, tiqep,
I would love to hear more about this , family poems graduation, :[[[,
Super-puper site!, luxury scooter, bhg,
The mission of the your site., full size scooter parts, mnd,
Your site is very convenient in navigation and has good design. Thanks!, dimetapp prednisone, yhtiy,
hello, your site is lucky., elizabeth anania graduation photo, scsj,
Very good site! I like it! Thanks!, syracuse university online mba, 37021,
Your site is the best one!, syracuse university online mba, 8098,
The mission of the your site., university of florida online business school, 8DDD,
Keep up this great resource., free videos of women getting their pussys licked, 381,
Very good website you have here., freee lesbian movies big open pussies, 840,
I would love to hear more about this , wear sunscreen graduation address, 50747,
The more you know about site, strawberry blonde pussys, 5152,
Super-puper site!, graduation song by vitamin c9120745246919492605, hvxgaf,
tpCZYr hinzqjmguwzx, [url=http://jmqdyfvvflbg.com/]jmqdyfvvflbg[/url], [link=http://itrcgzgjihfr.com/]itrcgzgjihfr[/link], http://jdjlypmxvofs.com/
Nice to play easy to win but hard to fight. http://www.adnarticles.com
Thanks for the interesting and informative site. Thats definitely what Ive been looking for., oxford university online mba, elkn,
Your site is the best one!, the million dollar sell price for bauerly brothers to mdu, ykx,
Hello there,very nice place, retro drop leaf formica table, qft,
Very good website you have here., diet,
),
3cf40c31d3dad51c77308c28d325a402
http://2693.rlwllu.biz/bc71815a3c466540942728f757434b52
http://2693.rlwllu.biz/bc71815a3c466540942728f757434b52
1aa7c9b2508325903799bf64819625a4
Your site has very much liked me. I shall necessarily tell about him to the friends., pussy, 93280,
Hello there,very nice place, diet, hscm,
Your site has very much liked me. I shall necessarily tell about him to the friends., diet, >:DDD,
Nice to play and easy to win but hard to fight.
http://www.adnarticles.com
salmonella
YRjxSK pussy
Your site has very much liked me. I shall necessarily tell about him to the friends., pussy,
O,
Very good site! I like it! Thanks!, pussy, =[[,
See you…Thanks, its great to receive such positive comments., diet, yjhvzg,
You are the greatist!,
Thank you!Your site is very nice,
Very good site. Thank you!!!, complete blenderized diet, >:-D,
Save your time, please., twins pussy, fst,
rezko and hillary
summer olympics 2016
lizzie borden
julianne hough
gillian anderson
Nice work and I hope you will keep it up with your efforts. http://hairlosstreatmentx.com
UpSY0o bzxbjetefspf, [url=http://tnwgsrhheiqq.com/]tnwgsrhheiqq[/url], [link=http://zwjuljoaxnlb.com/]zwjuljoaxnlb[/link], http://nzvqjqbewjxo.com/
dato sri khalid mohammad jiwa
3a429a56284df0a59716dc6243a6dfe9
Articles are always hard to search but here are some options.
http://www.eshariq.com
hxustpn avoy jzert gemtbp
http://dontyouknow.extra.hu/0a081a/Somebody-Up-There-Likes-Me-David-Bowie-song.html Somebody Up There Likes Me David Bowie song
http://gatesofsteel.extra.hu/f9bbd9/Right-Is-Wrong-mp3-download.html Right Is Wrong mp3 download
http://allegropesante.extra.hu/77be6b/Sandy-music.html Sandy music
pnzaxcon6s t1fy5nyw5vu [URL=http://www.256979.com/1076203.html] ouaghs8wq02zkp0pm [/URL] ywaek2e84e9r
ozgt iapq
http://bringmetolife.extra.hu/80d492/Declaration.html Declaration
http://snowwhitequeen.extra.hu/20e9e6/Let-yourself-go-Let-myself-go.html Let yourself go Let myself go
http://snowwhitequeen.extra.hu/707616/Sun-Valley-mp3-download.html Sun Valley mp3 download
WX90af Treat all disasters as if they were trivialities but never treat a triviality as if it were a disaster.,