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
Last revision Both sides next revision
litespeed_wiki:cache:developer_guide:cache-vary [2017/02/27 21:49]
Kevin Fwu [Vary Cookie]
litespeed_wiki:cache:developer_guide:cache-vary [2017/02/28 22:15]
Kevin Fwu [Vary Cookie]
Line 1: Line 1:
 ====== LiteSpeed Cache Vary ====== ====== LiteSpeed Cache Vary ======
  
-The Cache Vary is an important concept for caching. For simplicity'​s sake, the idea behind cache varies is that it allows ​the cache to save multiple ​copies ​of the same page.+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: ===== ===== 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.   - 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.+  - Global audience/​multi-lingual support. If the url must stay the same, it is possible to vary on the language.
  
 ===== Vary Types ===== ===== 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 ==== ==== 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.  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. 
Line 14: Line 25:
  
   * Request A has NO cookies.   * Request A has NO cookies.
 +    * ''​VARY = ""''​
   * Request B has the cookie my_cookie = Alabama   * Request B has the cookie my_cookie = Alabama
 +    * ''​VARY = "​my_cookie=Alabama"''​
   * Request C has the cookie my_cookie = California   * Request C has the cookie my_cookie = California
 +    * ''​VARY = "​my_cookie=California"''​
   * Request D has the cookie my_cookie = Alabama   * 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. 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 _lscache_vary as a vary cookie.+By default, LiteSpeed servers will recognize ​any cookie that starts with '_lscache_vary' ​as a vary cookie.
  
-==== Vary Value ==== +==== Vary Environment ​Value ==== 
-The Cache Vary Value is value used to vary request. ​Only one vary value may be used. If multiple are set, only the last one will be used.+The Cache Vary Value is an environment ​value to vary the request. ​The Vary Value differs from Vary Cookies in that the Value set is the value used. There is no key to check and 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 example listed above. Given the request'​s user agent, if it matches a mobile browser, one can set vary value = '​ismobile'​.+  * Request A has no vary value 
 +    * ''​VARY ​""​'
 +  * Request B sets vary value "ismobile
 +    * ''​VARY = "​ismobile"''​ 
 +  * Request C sets vary value "​US"​ 
 +    * ''​VARY = "​US"''​ 
 +  * Request D sets vary value "​US"​ 
 +    * ''​VARY = "​US"'​'
  
-===== How to instruct the server to use a vary =====+For this example, Requests C and D will get the same cached version (or another way to see it: request C can generate the cache entry for request D). Their cache entry will differ from requests A and B, both of which have a unique cache entry. So there are 3 total cache entries. 
 + 
 +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. 
 + 
 +===== ===== 
 + 
 +Vary Cookies and Vary Values can both be set. Setting one does not limit setting another.  
 + 
 +Suppose the server is instructed to vary on the cookies '​my_cookie'​ and '​my_cookie2'​. 
 + 
 +  * Request A has no cookies and no vary value is set. 
 +    * ''​VARY = ""''​ 
 +  * Request B has the cookie '​my_cookie=Alabama'​ and no vary value is set. 
 +    * ''​VARY = "​my_cookie=Alabama"''​ 
 +  * Request C has the cookie '​my_cookie=Alabama'​ and vary value '​ismobile'​ is set. 
 +    * ''​VARY = "​my_cookie=Alabama"​ + "​ismobile"''​ 
 +  * Request D has the cookies '​my_cookie=Alabama'​ and '​my_cookie2=Apple'​ and vary value '​ismobile'​ is set. 
 +    * ''​VARY = "​my_cookie=Alabama&​my_cookie2=Apple"​ + "​ismobile"''​ 
 +  * Request E has no cookies, but vary value '​ismobile'​ is set. 
 +    * ''​VARY = "​ismobile"''​ 
 + 
 +The five requests will all be served a different cache entry, because their final VARY results are different. 
 + 
 +===== 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 ====
  
-Rewrite Rules can be used to add a vary to the request when it comes in.+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 === === Vary Cookie ===
Line 40: Line 87:
 This example rewrite rule will instruct the server to check for the "​my_cookie"​ cookie NAME and if it exists, vary on it. 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. +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: Example:
Line 49: Line 96:
 Example: Example:
  
-''​RewriteCond %{HTTP_USER_AGENT} Mobile|Android|Silk/​|Kindle|BlackBerry|Opera\ Mini|Opera\ Mobi [NC] +''​RewriteCond %{HTTP_USER_AGENT} Mobile|Android|Silk/​|Kindle|BlackBerry|Opera\ Mini|Opera\ Mobi [NC]''​ 
-RewriteRule .* - [E=Cache-Control:​vary=ismobile]''​+''​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.+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=". These are how to differentiate the two.+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 Header ====
Line 62: Line 109:
 Response headers can be used to instruct the server to cache the page with the listed varies in mind. 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 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 cache smarter, only the password protected pages will use the vary.+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: Example header:
  
-''​X-LiteSpeed-Vary:​ cookie=my_cookie,​value=ismobile''​+''​X-LiteSpeed-Vary:​ cookie=my_cookie,​cookie=my_cookie2''​
  
-For the pages with that vary headerthe server will compare against that cookie ​and value.+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''​
  
-The Vary Value response header has a special meaning, because requests do not necessarily come in with a vary value. ​This response header ​is meant to be used in conjunction with the vary value rewrite rule. An example scenario using this case would be if a browser was initially detected as a desktop browser, but was actually loading a mobile view. Using the response header, ​the server can be notified that the page should actually be cached with the "ismobile" ​value.+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: 2020/07/31 19:35
  • by Lisa Clarke