This is an old revision of the document!


LiteSpeed Cache Vary

The Cache Vary is an important concept for caching. For simplicity's sake, the idea behind cache varies is that they allow the cache to save multiple versions of the same URL.

  1. Differences between Desktop and Mobile views. Specifically, the html for the desktop and mobile views do not match. This could be because of a non-responsive theme, different widgets loading, or otherwise.
  2. Global audience/multi-lingual support. If the url must stay the same, it is possible to vary on the language.

LiteSpeed Servers can vary on cookies. The server needs to be instructed about the cookie NAME to vary on. The server will then use the cookie VALUE as part of the cache key.

For Example, suppose the server is instructed to vary on the cookie 'my_cookie'.

  • Request A has NO cookies.
  • Request B has the cookie my_cookie = Alabama
  • Request C has the cookie my_cookie = California
  • Request D has the cookie my_cookie = Alabama

For this example, Requests B and D will get the same cached version (or another way to see it: request B can generate the cache entry for request D). Their cache entry will differ from requests A and C, both of which have a unique cache entry. So there are 3 total cache entries.

By default, LiteSpeed servers will recognize any cookie that starts with '_lscache_vary' as a vary cookie.

Vary Environment Value

The Cache Vary Value is a value used to vary a request. Only one vary value may be used. If multiple environment values are set, only the last one will be used.

An example use case is the separate mobile view use case listed above. Given the request's user agent, if it matches a mobile browser, one can set vary value = 'ismobile'. This will be covered in the next section below.

Custom varies should be used if the contents of the site change depending on controllable circumstances, such as when a User Agent matches a certain pattern or if a cookie exists and has a specific value. Custom cookie varies can also be used to prevent conflicting login situations, as described here.

Rewrite Rules

Rewrite Rules can be used to vary the request when it comes in.

Vary Cookie

Example:

RewriteRule .? - [E=Cache-Vary:my_cookie]

This example rewrite rule will instruct the server to check for the “my_cookie” cookie NAME and if it exists, vary on it.

As of LSWS version 5.1.x and OLS 1.4.x, it is possible to add more vary cookies to the rewrite rule.

Example:

RewriteRule .* - [E=“cache-vary:xf_style_id,xf_language_id”]

Vary Value

Example:

RewriteCond %{HTTP_USER_AGENT} Mobile|Android|Silk/|Kindle|BlackBerry|Opera\ Mini|Opera\ Mobi [NC] RewriteRule .* - [E=Cache-Control:vary=ismobile]

This example rewrite rule checks the user agent for a mobile browser agent. If it matches, it will add the vary value 'ismobile' to the request.

NOTICE: Examine the examples closely. Note that for the vary cookie, it uses E=Cache-Vary:, and for the vary value, it uses E=Cache-Control:vary=.

Response Header

Response headers can be used to instruct the server to cache the page with the listed varies in mind.

Note that the response headers should be used if the cache vary is meant to be set on a per page basis. For example, the WordPress plugin uses the response header to vary password protected pages on the password cookie. There is no reason to vary on the password cookie on non password protected pages, so to save space, only the password protected pages will use the vary.

Vary Cookie

Adding a vary header with 'cookie=' instructs the server to check for the specified vary cookie(s) for the current URL only.

Example header:

X-LiteSpeed-Vary: cookie=my_cookie,cookie=my_cookie2

This response header will add two vary cookies, 'my_cookie' and 'my_cookie2', to the list of varies to check for this URL. On the next request for this URL, the server will check for the vary cookies set by the rewrite rules in addition to the two cookies above as if they were part of the rewrite rule.

Vary Value

Adding a vary header with 'value=' instructs the server to add the environment value to the cache entry. This is useful for situations where the environment value should be set, but the rewrite rule missed setting it.

Example header:

X-LiteSpeed-Vary: value=ismobile

This response header will add the 'ismobile' environment value to the cache key for the URL. This may be set when desktop and mobile views should be cached separately, but the rewrite rule did not match a mobile user agent and the web application is building a mobile page. This vary header will cache the page as a mobile page (as determined by the environment value).

NOTE: This will only somewhat alleviate the problem described in the example. If the rewrite rule does not match the web application's definition of a mobile user agent, there will still be issues. The proper fix for this situation would be to correct the rewrite rule to match the web application.

It is also possible to set both vary cookies and vary values using a single response header, like so:

X-LiteSpeed-Vary: cookie=my_cookie,value=ismobile

This response header will vary the URL on the 'my_cookie' cookie and add the 'ismobile' environment value to the current response.

  • Admin
  • Last modified: 2017/02/28 14:58
  • by Ron Saad