How To Set Http Header Vary Value Correct To Make Cache Work Effective

My website dev2qa.com uses Ezoic caching function to cache all my website pages. This can improve my website page access speed. Ezoic proxy server will create a copy of my website page and save those web page copies in their data center all over the world.

So when a client requests a web page, and if the web page has been saved by Ezoic cache before, Ezoic will return the saved web page back to the client instead of pass the request to the original web server. This way can improve web page response speed, save network bandwidth and reduce original web server resource costs.

1. What Does HTTP Header ‘Vary’ Means.

  1. The above process is just the general process of cache proxy. But when a client request comes to the cache proxy,  the returned cached web page is not only decided by the request URL, it is also decided by an HTTP header Vary‘s value.
  2. The HTTP header Vary‘s value is a serial of other HTTP header’s name separated by comma (,). It is used as an extra condition for loading and returns cached web pages.
  3. For example, if the HTTP vary header value is  vary: Accept-Encoding,User-Agent. And when a client requests a web page http://www.dev2qa.com/1.html to the cache proxy, the cache proxy will return the cached 1.html web page only when the requesting client has the same Accept-Encoding and User-Agent header value.
  4. If another client ( which has a different User-Agent header value ) requests the same web page http://www.dev2qa.com/1.html to the cache proxy,  the proxy will load and return a different User-Agent header value mapped cached web page to the client even if the web page URL is same and have been cached before.
  5. So the cache proxy will save the same URL web page contents many times and maintain mappings between request-URL + HTTP-header-value to web page content.
  6. For example, the client request’s HTTP header User-Agent’s value can be Mozilla, Internet Explorer, Chrome, Safari, etc. And suppose each user agent can have 5 subversions, so there should be 20 copies of the same URL web page content are saved on the cache proxy server. This is very low efficiency and costs more resources.

2. Bad Http Header Vary’s Value Example.

  1. Now you should understand what does HTTP header ‘vary’ means. And I will tell you what is the bad vary header’s value below.
  2. vary: Accept-Encoding,User-Agent,Cookie : If you find your HTTP response vary header’s value like this, then this is even worse than vary: Accept-Encoding,User-Agent.
  3. Because each requesting client may have a different Cookie, so this can make the cache very very very low efficiency. The cached web page is only used for the client request which has the same Accept-Encoding, User-Agent, and Cookie value.
  4. So do not include Cookie, User-Agent in the HTTP ‘vary’ header value, if you find them then remove it.
  5. But some programs like the WordPress WP Super Cache plugin will add Cookie in the HTTP ‘vary’ header by default for login user caching, we will tell you how to remove it in such cases in another article.
  6. vary: *: * is the worst HTTP ‘vary’ header value. This can make the cache proxy stop functioning. Every request will be passed to the original web server. So do not use * in HTTP header vary‘s value.

2. Best Http Header Vary’s Value Example.

  1. vary: Accept-Encoding: We commonly use Accept-Encoding in the HTTP header vary‘s value.
  2. Accept-Encoding‘s value will tell the webserver which content compression algorithm the client is using and can understand.
  3. Then web server will return that encoding compressed web page back to the client. If two or more client requests use the same accept-encoding, then the same cached web page will be returned when they request the same URL page.
    http-cache-vary-header

Reference

  1. How To View HTTP Headers, Cookies In Google Chrome, Firefox, Internet Explorer

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.