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:litespeed:cache [2013/11/05 15:52]
Michael [How to enable LiteSpeed Cache]
litespeed_wiki:litespeed:cache [2017/01/03 19:56] (current)
Michael Alegre Added Redirect.
Line 1: Line 1:
-====== Introduction ======+~~REDIRECT>​litespeed_wiki:​cache:​lscache~~ 
 + 
 +====== ​LiteSpeed Cache (LSCache) ​Introduction ======
    
-LiteSpeed has made a built-in cache functionality (an advanced feature for Enterprise 2+CPU license) available ​since version 4.0 (March, 2009), ​and keeps improving throughout the later 4.0.x releases ​and 4.1RC releases. As of 4.1.1 (May, 2011), private cache support is added.+LiteSpeed has created ​a built-in cache functionality (an advanced feature for Enterprise 2+CPU license. Available ​since version 4.0 (March, 2009), ​its performance ​keeps improving throughout the later 4.0.x and 4.1RC releases. As of 4.1.1 (May, 2011), private cache support is added.
  
-LiteSpeed cache has similar ​features ​as Apache mod_cache but implemented in a more efficient way, and works like Varnish. It is an output cache for dynamic contents, so the usage does not limit to PHP pages. Unlike Varnish, ​it is built into LiteSpeed web server, eliminating one layer of reverse proxy. ​Hence more efficient ​for static contents. The uniqueness of LiteSpeed cache is that it uses rewrite rules (either in configuration files or .htaccess) to control its behavior for maximum flexibility. ​+LiteSpeed'​s ​cache features ​are very similar to those in Apache mod_cachebut implemented in a more efficient way. LiteSpeed cache works like Varnish. It is an output cache for dynamic contents, so the usage is not limited ​to PHP pages. Unlike Varnish, ​LiteSpeed cache is built into LiteSpeed'​s ​web server, ​thus eliminating one layer of reverse proxy. ​This translates into higher efficieny ​for static contents. The uniqueness of LiteSpeed cache is that it uses rewrite rules (either in configuration files or .htaccess) to control its behavior for maximum flexibility. ​
  
-A general way to enable LiteSpeed Cache through rewrite rules is to designate a signature token (such as cookie) in a page (or any pagein most cases to tell LSWS that cache can be enabled and TTL(Time To Live) of the cache freshness ​however ​acceptable. Generating ​cookie ​usually requires application code modification. ​Refer to [[http://​www.litespeedtech.com/​support/​forum/​showthread.php?​t=4679]] for example. It is worth noting that the cache function **SHOULD ONLY** be used for the pages that are **supposed to be cached or cache friendly**. Abuse use of this feature would result in performance degradation and/or other unexpectancies.+A general way to enable LiteSpeed Cache through rewrite rules is to designate a signature token (such as cookie) in any pagein most cases it will tell LSWS that cache can be enabled and that the TTL(Time To Live) of the cache freshness ​will remain ​acceptable. Generating ​cookies ​usually requires application code modification. ​Please refer to [[http://​www.litespeedtech.com/​support/​forum/​showthread.php?​t=4679]] for examples. It is worth noting that the cache function **SHOULD ONLY** be used for pages that are **supposed to be cached or cache friendly**. Abuse use of this feature would result in performance degradation and/or other unexpectancies.
  
-Since 4.1.1 (May, 2011), private cache support ​is added. Private cache is for caching resources for individual specifically instead of public shared. The purposes are:+Since 4.1.1 (May, 2011), private cache support ​has been added. Private cache is for caching resources for individual specifically instead of public shared. The purposes are:
   * maintain a persistent cache for applications that do not have a shared cache.   * maintain a persistent cache for applications that do not have a shared cache.
   * maintain a private persistent cache for specific groups of documents that are not to be shared among other applications.   * maintain a private persistent cache for specific groups of documents that are not to be shared among other applications.
Line 45: Line 47:
 ====== How to enable LiteSpeed Cache ====== ====== How to enable LiteSpeed Cache ======
  
-Here are the steps to enable LSWS built-in cache:+Although there is a advanced setting [[http://​litespeedtech.com/​docs/​webserver/​config/​cache#​cachePolicy:​enableCache|"​Enable Cache"​]] ​ in LSWS Wed Admin Console on both server and virtual host level, "​No"​ is always recommended since globally enabled public cache may cause problem when URLs change or other changes on the domain. We recommend to enable cache through rewrite rules. ​Here are the steps to enable LSWS built-in cache:
  
 **1. Enable cache function at Server level in LSWS** **1. Enable cache function at Server level in LSWS**
Line 52: Line 54:
    * Manually create the directory from shell, and give write permission for litespeed/​lshttpd processes (assume running as "​nobody"​ here):    * Manually create the directory from shell, and give write permission for litespeed/​lshttpd processes (assume running as "​nobody"​ here):
  
-    ​# mkdir /​tmp/​diskcache
  +  ​# mkdir /​tmp/​diskcache
  
-    # chown nobody:​nobody /​tmp/​diskcache
  ​      +  # chown nobody:​nobody /​tmp/​diskcache
  ​      
-    # chmod 700 /​tmp/​diskcache+  # chmod 700 /​tmp/​diskcache
  
 **2. Set Default cache policy at LSWS Server level** **2. Set Default cache policy at LSWS Server level**
Line 60: Line 62:
 LSWS Web Admin Console->​Virtual Hosts->​mytest->​Cache -> Cache Policy LSWS Web Admin Console->​Virtual Hosts->​mytest->​Cache -> Cache Policy
  
-    ​Enable Cache:No +  ​Enable Cache:No 
-    Cache Request with Query String:​Yes  +  Cache Request with Query String:​Yes  
-    Cache Request with Cookie:No +  Cache Request with Cookie:No 
-    Cache Response with Cookie:​No  +  Cache Response with Cookie:​No  
-    Ignore Request Cache-Control:​No
  +  Ignore Request Cache-Control:​No
  
-    Ignore Response Cache-Control:​No +  Ignore Response Cache-Control:​No 
-    Enable Private Cache: Yes* +  Enable Private Cache: Yes* 
-    Private Cache Expire Time (seconds): 120+  Private Cache Expire Time (seconds): 120
  
 **Note:** **Note:**
Line 76: Line 78:
  
 **3. For imported Apache vhost:** **3. For imported Apache vhost:**
-Cache can be turned on/off with "​CacheEnable"​ , "​CacheDisable"​ directives in Apache config file: .htaccess (preferred) or httpd.conf, etc. Nothing needs to be done for the cache policy. It will assume the same default settings at LSWS Server level. Rewrite rules can be placed in httpd.conf or in-directory .htaccess file as shown in step 5 below.+Cache can be turned on/off with "​CacheEnable"​ , "​CacheDisable"​ directives in Apache config file: .htaccess (preferred) or httpd.conf, etc. Nothing needs to be done for the cache policy. It will assume the same default settings at LSWS Server level. ​ 
 +  * To enable private cache, you can add to .htaccess under document root of the web site, 
 + 
 +  <​IfModule LiteSpeed>​  
 +  CacheEnable private / 
 +  </​IfModule>​  
 + 
 +  * To disable private cache,  
 + 
 +  <​IfModule LiteSpeed>​  
 +  CacheDisable private / 
 +  </​IfModule>​  
 + 
 +  * To enable public cache, 
 + 
 +  <​IfModule LiteSpeed>​  
 +  CacheEnable public / 
 +  </​IfModule>​  
 + 
 +  * To disable public cache, 
 + 
 +  <​IfModule LiteSpeed>​  
 +  CacheDisable public / 
 +  </​IfModule>​  
 + 
 +To enable/​disable cache for certain URL, just replace "/"​ with desired URL. 
 + 
 +Rewrite rules can be placed in httpd.conf or in-directory .htaccess file as shown in step 5 below.
  
 **Note:​** ​ **Note:​** ​
Line 87: Line 116:
 Setup Cache Policy (LSWS Web Admin Console->​Virtual Hosts->​mytest->​Cache -> Cache Policy) Setup Cache Policy (LSWS Web Admin Console->​Virtual Hosts->​mytest->​Cache -> Cache Policy)
  
-    ​Enable Cache:No +  ​Enable Cache:No 
-    Cache Request with Query String:​yes  +  Cache Request with Query String:​yes  
-    Cache Request with Cookie:Not Set  +  Cache Request with Cookie:Not Set  
-    Cache Response with Cookie:Not Set  +  Cache Response with Cookie:Not Set  
-    Ignore Request Cache-Control:​No
t Set  +  Ignore Request Cache-Control:​No
t Set  
-    Ignore Response Cache-Control:​Not Set +  Ignore Response Cache-Control:​Not Set 
-    Enable Private Cache: Not Set +  Enable Private Cache: Not Set 
-    Private Cache Expire Time (seconds): Not Set+  Private Cache Expire Time (seconds): Not Set
  
 **Note:** those with not set, settings at LSWS Server Level will be assumed. ​ **Note:** those with not set, settings at LSWS Server Level will be assumed. ​
Line 100: Line 129:
 **5. Create/Add rewrite rules for caching** **5. Create/Add rewrite rules for caching**
  
-    ​RewriteEngine On +  ​RewriteEngine On 
-    ## cache should be available for HEAD or GET requests +  ## cache should be available for HEAD or GET requests 
-    RewriteCond %{REQUEST_METHOD} ^HEAD|GET$ +  RewriteCond %{REQUEST_METHOD} ^HEAD|GET$ 
-    ## select which pages to cache +  ## select which pages to cache 
-    RewriteCond %{HTTP_COOKIE} page_contain_cachetoken=yes +  RewriteCond %{HTTP_COOKIE} page_contain_cachetoken=yes 
-    # with other condition +  # with other condition 
-    
RewriteCond %{QUERY_STRING} !s=[a-fA-F0-9]{32} +  
RewriteCond %{QUERY_STRING} !s=[a-fA-F0-9]{32} 
-    
# excluding certain URLs +  
# excluding certain URLs 
-    RewriteCond %{REQUEST_URI} !/​(login|register|usercp|private|profile|cron|image)\.php$
 +  RewriteCond %{REQUEST_URI} !/​(login|register|usercp|private|profile|cron|image)\.php$
 
-    # cache for 2 mins for php pages only +  # cache for 2 mins for php pages only 
-    RewriteRule /​(.*\.php)?​$ – [L,​E=Cache-Control:​max-age=120]+  RewriteRule /​(.*\.php)?​$ – [L,​E=Cache-Control:​max-age=120]
  
-    ​# for those not met above condition, enable private cache. +  ​# for those not met above condition, enable private cache. 
-    RewriteCond %{REQUEST_METHOD} ^HEAD|GET$ +  RewriteCond %{REQUEST_METHOD} ^HEAD|GET$ 
-    ## select which pages to serve from private cache +  ## select which pages to serve from private cache 
-    RewriteCond %{HTTP_COOKIE} !page_contain_cachetoken=yes +  RewriteCond %{HTTP_COOKIE} !page_contain_cachetoken=yes 
-    # with other condition +  # with other condition 
-    
RewriteCond %{QUERY_STRING} !s=[a-fA-F0-9]{32} +  
RewriteCond %{QUERY_STRING} !s=[a-fA-F0-9]{32} 
-    
# excluding certain URLs +  
# excluding certain URLs 
-    RewriteCond %{REQUEST_URI} !/​(login|register|usercp|private|profile|cron|image)\.php$
 +  RewriteCond %{REQUEST_URI} !/​(login|register|usercp|private|profile|cron|image)\.php$
 
-    # private cache for however long set in cache policy for php pages only +  # private cache for however long set in cache policy for php pages only 
-    RewriteRule /​(.*\.php)?​$ – [L,​E=Cache-Control:​private]+  RewriteRule /​(.*\.php)?​$ – [L,​E=Cache-Control:​private]
  
  
Line 127: Line 156:
         * .htaccess (last line needs to changed as follows:)         * .htaccess (last line needs to changed as follows:)
  
-    ​RewriteRule (.*\.php)?$ – [L,​E=Cache-Control:​max-age=120] +  ​RewriteRule (.*\.php)?$ – [L,​E=Cache-Control:​max-age=120] 
-    RewriteRule (.*\.php)?$ – [L,​E=Cache-Control:​private]+  RewriteRule (.*\.php)?$ – [L,​E=Cache-Control:​private]
  
  * VirutalHost secion in Apache config file  * VirutalHost secion in Apache config file
Line 143: Line 172:
  
 Since 4.1.1 release, LiteSpeed outputs a response header “X-LiteSpeed-Cache:​ hit,​private” if a request is served from private cache. Since 4.1.1 release, LiteSpeed outputs a response header “X-LiteSpeed-Cache:​ hit,​private” if a request is served from private cache.
 +
 +**Note:** LSCache hit rate is calculated based on all files served. Many of the files served by LSWS, like CSS or HTML, are intentionally not cached by LSCache. Because these files are included in the LSCache hit rate calculation,​ the hit rate may sometimes look much lower than one might expect.
  
 **8. Cache clean up (Optional)** **8. Cache clean up (Optional)**
Line 156: Line 187:
 ==== Cache everything for 2 mins (Simple!) ==== ==== Cache everything for 2 mins (Simple!) ====
  
-    ​RewriteEngine On +  ​RewriteEngine On 
-    RewriteRule cacheable/​(.*\.php)?​$ - [L,​E=cache-control:​max-age=120]+  RewriteRule cacheable/​(.*\.php)?​$ - [L,​E=cache-control:​max-age=120]
  
 **Note:** Only cache *.php files in "​cacheable"​ directory. As pointed out previously, it is a good practice to cache only the files that are supposed to be cached. **Note:** Only cache *.php files in "​cacheable"​ directory. As pointed out previously, it is a good practice to cache only the files that are supposed to be cached.
Line 164: Line 195:
 ==== Only cache the page with certain signature ==== ==== Only cache the page with certain signature ====
  
-    ​RewriteEngine On +  ​RewriteEngine On 
-    RewriteCond %{REQUEST_FILENAME} !-f +  RewriteCond %{REQUEST_FILENAME} !-f 
-    RewriteCond %{REQUEST_FILENAME} !-d +  RewriteCond %{REQUEST_FILENAME} !-d 
-    RewriteCond %{REQUEST_URI} !^/​index.php +  RewriteCond %{REQUEST_URI} !^/​index.php 
-    RewriteCond %{REQUEST_URI} (/​|\.php|\.html|\.htm|\.feed|\.pdf|\.raw|/​[^.]*)$ ​ [NC] +  RewriteCond %{REQUEST_URI} (/​|\.php|\.html|\.htm|\.feed|\.pdf|\.raw|/​[^.]*)$ ​ [NC] 
-    RewriteRule (.*) index.php ​+  RewriteRule (.*) index.php ​
  
-    ​# this part is for public cache. +  ​# this part is for public cache. 
-    RewriteCond %{REQUEST_METHOD} ^HEAD|GET$ +  RewriteCond %{REQUEST_METHOD} ^HEAD|GET$ 
-    RewriteCond %{HTTP_COOKIE} !loginuser +  RewriteCond %{HTTP_COOKIE} !loginuser 
-    RewriteCond %{ORG_REQ_URI} !^/​index.php$ +  RewriteCond %{ORG_REQ_URI} !^/​index.php$ 
-    RewriteCond %{ORG_REQ_URI} !^/​administrator/​ +  RewriteCond %{ORG_REQ_URI} !^/​administrator/​ 
-    RewriteCond %{ORG_REQ_URI} (\.php|\.html|\.htm|\.feed|\.pdf|\.raw|/​[^.]*)$ ​ [NC] +  RewriteCond %{ORG_REQ_URI} (\.php|\.html|\.htm|\.feed|\.pdf|\.raw|/​[^.]*)$ ​ [NC] 
-    RewriteRule .* - [E=Cache-Control:​max-age=300,​L]+  RewriteRule .* - [E=Cache-Control:​max-age=300,​L]
  
 **Note:** This example demonstrate how LSWS cache rewrite rules fit in application'​s rewrite rules. The first part is from application (such as joomla). Essentially everything goes through index.php (joomla framework) to process. ​ **Note:** This example demonstrate how LSWS cache rewrite rules fit in application'​s rewrite rules. The first part is from application (such as joomla). Essentially everything goes through index.php (joomla framework) to process. ​
Line 192: Line 223:
  
  
-    ​RewriteEngine On +  ​RewriteEngine On 
-    RewriteCond %{REQUEST_FILENAME} !-f +  RewriteCond %{REQUEST_FILENAME} !-f 
-    RewriteCond %{REQUEST_FILENAME} !-d +  RewriteCond %{REQUEST_FILENAME} !-d 
-    RewriteCond %{REQUEST_URI} !^/​index.php +  RewriteCond %{REQUEST_URI} !^/​index.php 
-    RewriteCond %{REQUEST_URI} (/​|\.php|\.html|\.htm|\.feed|\.pdf|\.raw|/​[^.]*)$ ​ [NC] +  RewriteCond %{REQUEST_URI} (/​|\.php|\.html|\.htm|\.feed|\.pdf|\.raw|/​[^.]*)$ ​ [NC] 
-    RewriteRule (.*) index.php ​+  RewriteRule (.*) index.php ​
  
-    ​# this part is for private cache, note that HTTP_COOKIE is for loginuser +  ​# this part is for private cache, note that HTTP_COOKIE is for loginuser 
-    RewriteCond %{REQUEST_METHOD} ^HEAD|GET$ +  RewriteCond %{REQUEST_METHOD} ^HEAD|GET$ 
-    RewriteCond %{HTTP_COOKIE} loginuser +  RewriteCond %{HTTP_COOKIE} loginuser 
-    RewriteCond %{ORG_REQ_URI} !^/​index.php$ +  RewriteCond %{ORG_REQ_URI} !^/​index.php$ 
-    # there is no need to exclude admin area. it can be private cached. +  # there is no need to exclude admin area. it can be private cached. 
-    # RewriteCond %{ORG_REQ_URI} !^/​administrator/​ +  # RewriteCond %{ORG_REQ_URI} !^/​administrator/​ 
-    RewriteCond %{ORG_REQ_URI} (\.php|\.html|\.htm|\.feed|\.pdf|\.raw|/​[^.]*)$ ​ [NC] +  RewriteCond %{ORG_REQ_URI} (\.php|\.html|\.htm|\.feed|\.pdf|\.raw|/​[^.]*)$ ​ [NC] 
-    RewriteRule .* - [E=Cache-Control:​private,​L]+  RewriteRule .* - [E=Cache-Control:​private,​L]
  
 The ruleset indicates that LSWS only private cache  The ruleset indicates that LSWS only private cache 
Line 232: Line 263:
   - put the following entry in .htaccess under document root of the domain/​virtual host in question.   - put the following entry in .htaccess under document root of the domain/​virtual host in question.
  
-    ​RewriteEngine On +  ​RewriteEngine On 
-    RewriteRule .* - [E=Cache-Control:​no-cache] ​+  RewriteRule .* - [E=Cache-Control:​no-cache] ​
  
 or put the following in vhost section of httpd.conf or put the following in vhost section of httpd.conf
  
-    ​CacheDisable private+  ​CacheDisable private
  
  • Admin
  • Last modified: 2013/11/05 15:52
  • (external edit)