设为首页 收藏本站
查看: 1118|回复: 0

[经验分享] Understanding the HTTP Vary Header and Caching Proxies (Squid, etc.)

[复制链接]
发表于 2015-9-3 09:42:03 | 显示全部楼层 |阅读模式
  Understanding the HTTP Vary Header and Caching Proxies (Squid, etc.) - Mark S. Kolich

Understanding the HTTP Vary Header and Caching Proxies (Squid, etc.)

  
  By Mark Kolich on September 25, 2010 12:10 AM
  
  | No TrackBacks


I never paid much attention to the HTTP Vary header.  In fact, I've been fortunate enough to avoid it for this long and never really had to care much about it.  Well, it turns out when you're configuring a high-performance reverse proxy, understanding the Vary header and what it means to your reverse proxy caching policies is absolutely crucial.
Here's an interesting problem I recently solved that dealt with Squid, Apache, and that elusive Vary response header ...

1 - The Vary Basics
Popular caching proxies, like Squid, usually generate a hash of the request from a number of inputs including the URI and the contents of the Vary response header.  When a caching proxy receives a request for a resource, it gathers these inputs, generates a hash, then checks its cache to see if it already has a resource sitting on disk, or in memory, that matches the computed hash.  This is how Squid, and other caching proxies, fundamentally know if they have a cache HIT or MISS (e.g., can Squid return the content it has cached or does it need to revalidate the request against the destination server).
That in mind, you can probably see how the Vary header is quite important when a caching proxy is looking for a cache HIT or MISS.  The Vary header is a way for the web-server to tell any intermediaries (caching proxies) what they should use, if necessary, to figure out if the requested resource is fresh or stale.  Sample Vary headers include:

Vary: Accept-Encoding
Vary: Accept-Encoding,User-Agent
Vary: X-Some-Custom-Header,Host
Vary: *
  
According to the HTTP spec, "the Vary field value indicates the set of request-header fields that fully determines, while the response is fresh, whether a cache is permitted to use the response to reply to a subsequent request without revalidation."  Yep, that's pretty important (I discovered this the hard way).

2 - The Caching Problem
I configured Squid to act as a round-robin load balancer and caching proxy, sitting in front of about four Apache web-servers.  Each Apache web-server was running a copy of my web-application, which I intended to have Squid cache where possible.  Certain requests, were for large JSON objects, and I explicitly configured Squid to cache requests ending in .json for 24-hours.
I opened a web-browser and visited a URL I expected to be cached (should have already been in the cache from a previous request, notice the HIT) ...


GET /path/big.json HTTP/1.1
Host: app.kolich.local
User-Agent: Firefox
HTTP/1.0 200 OK
Date: Fri, 24 Sep 2010 23:09:32 GMT
Content-Type: application/json;charset=UTF-8
Content-Language: en-US
Vary: Accept-Encoding,User-Agent
Age: 1235
X-Cache: HIT from cache.kolich.local
X-Cache-Lookup: HIT from cache.kolich.local:80
Content-Length: 25090
Connection: close
  
Ok, looks good!  I opened a 2nd web-browser on a different machine (hint: with a different User-Agent) and tried again.  This time, notice the X-Cache: MISS ...


GET /path/big.json HTTP/1.1
Host: app.kolich.local
User-Agent: Chrome
HTTP/1.0 200 OK
Date: Fri, 24 Sep 2010 23:11:45 GMT
Content-Type: application/json;charset=UTF-8
Content-Language: en-US
Vary: Accept-Encoding,User-Agent
Age: 4
X-Cache: MISS from cache.kolich.local
X-Cache-Lookup: MISS from cache.kolich.local:80
Content-Length: 25090
Connection: close
  
Wow, look at that.  I requested exactly the same resource, just from a different browser, and I saw a cache MISS.  This is obviously not what I want, I need the same cached resource to be served up from the cache regardless of who's making the request.  If left alone, this is only caching a response per User-Agent, not globally per resource.

3 - Solution: Check Your Vary Headers
Remember how I said the contents of the Vary header are important for caching proxies?
In both requests above, note the User-Agent request headers and the contents of the Vary response headers.  Although each request was for exactly the same resource, Squid determined that they were very different as far as its cache was concerned.  How did this happen?  Well, take a peek at a Vary response header:


Vary: Accept-Encoding,User-Agent
  
This tells Squid that the request URI, the Accept-Encoding request header, and the User-Agent request header should be included in a hash when determining if an object is available in its cache, or not.  Obviously, any reasonable hash of (URI, Accept-Encoding, "Firefox") should not match the hash of (URI, Accept-Encoding, "Chrome").  Hence why Squid seemed to think the request was for different objects!
To fix this, I located the source of the annoying "User-Agent" addition to my Vary response header, which happened to come from Apache's very own mod_deflate module.  The recommended mod_deflate configuration involves appending "User-Agent" to the Vary response header on any response that is not compressed by mod_deflate.  I don't really see why this is necessary, but the Apache folks seemed to think this was important.  Here's the relevant lines from the Apache suggested mod_deflate configuration:


SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png|ico)$ no-gzip dont-vary
Header append Vary User-Agent env=!dont-vary
  
In any event, I removed the 2nd line above, restarted Apache and Squid began caching beautifully regardless of which client issued the request.  Essentially, I told Squid to stop caring about the User-Agent by removing "User-Agent" from my Vary response header, and problem solved!
The joys of HTTP.

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-108932-1-1.html 上篇帖子: 关于squid禁止某些站点的访问的控制 (acl语句) 下篇帖子: squid总结
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表