Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
litespeed_wiki:cache:developer_guide:cache-vary [2017/02/28 21:45]
Kevin Fwu [Vary Cookie]
litespeed_wiki:cache:developer_guide:cache-vary [2020/07/31 19:35] (current)
Lisa Clarke Redirect to new Documentation Site
Line 1: Line 1:
-====== LiteSpeed Cache Vary ====== +~~REDIRECT>​https://docs.litespeedtech.com/lscache/devguide/advanced/~~
- +
-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. +
- +
-Using the public cache key as an example, KEY = Host + URI + Query String + VARY +
- +
-This article describes the VARY portion of the cache key. +
- +
-===== Examples of reasons to use cache vary===== +
-  - 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. +
-  - Global audience/multi-lingual support. If the url must stay the same, it is possible to vary on the language. +
- +
-===== Vary Types ===== +
- +
-There are two types of cache varies, vary cookie and vary value. Both are used when determining which version to return. Setting a Vary Cookie will notify the server about which cookies to keep an eye on. This will not actually set a cookie. Setting a Vary Value will set an environment value strictly for the use of varying the request. +
- +
-Using the earlier cache key equation, VARY can be expanded to: +
- +
-VARY = VARY_COOKIE + VARY_VALUE +
- +
-==== Vary Cookie ==== +
-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. +
-    * VARY = ''​ +
-  * Request B has the cookie my_cookie = Alabama +
-    * VARY = '​my_cookie=Alabama'​ +
-  * Request C has the cookie my_cookie = California +
-    * VARY = '​my_cookie=California'​ +
-  * Request D has the cookie my_cookie = Alabama +
-    * VARY = '​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. +
- +
-===== How to instruct the server to use a custom vary ===== +
- +
-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 [[litespeed_wiki:​cache:​common:​logged-in-cookie-conflicts|here]]. +
- +
-==== Rewrite Rules ==== +
- +
-Rewrite Rules can be used to vary the request when it comes in. Varies set by Rewrite Rules will be forwarded to the web application via environment variables. In PHP, these are ''​$_SERVER['​LSCACHE_VARY_COOKIE'​]''​ and ''​$_SERVER['​LSCACHE_VARY_VALUE'​]''​ respectively. For cookies, it will be the list of cookie names that the server checked. For value, it will be the value used. This may be empty if no vary values are set. +
- +
-=== 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 multiple 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 agentIf 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 environment 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 21:45
  • by Kevin Fwu