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:no-plugin-setup-guidline [2017/05/11 21:15]
Michael Alegre [Rewrite Examples for applications]
litespeed_wiki:cache:no-plugin-setup-guidline [2020/08/13 17:16]
Jackson Zhang [How to purge cache by URL]
Line 1: Line 1:
 ====== LiteSpeed Cache (LSCache) Introduction ====== ====== LiteSpeed Cache (LSCache) Introduction ======
    
-LiteSpeed has created a built-in cache functionality for it's Enterprise Edition web server. LSCache is included free in 2-CPUlicenses and as an add-on for other licenses. (Available since version 4.0)+LiteSpeed has created a built-in cache functionality for it's Enterprise Edition web server. LSCache is included free in the Free Starter, Site Owner, and Web Host licenses, as well as the 8-CPU legacy ​licensesand is available ​as an add-on for the other legacy ​licenses. (Available since version 4.0)
  
 LSCache'​s features are very similar to those in Apache mod_cache, but 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. ​ LSCache'​s features are very similar to those in Apache mod_cache, but 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. ​
  
-If there is a LiteSpeed cache plugin available for your web applications (WordPress, WooCommerce,​ Magento, Xenforo), you should follow ​**[[litespeed_wiki:​cache#​litespeed_cache_plugins_by_application|the corresponding section]]** to set up your cache. In these cases, following this wiki is not recommended as it was written for more general non-application-specific caching. As a result, it is only applicable when a LiteSpeed cache plugin for your application is not available or you have very customized code base application.+If there is a LiteSpeed cache plugin available for your web applications (WordPress, WooCommerce,​ Magento, Xenforo), you should follow [[litespeed_wiki:​cache#​litespeed_cache_plugins_by_application|the corresponding section]] to set up your cache. In these cases, following this wiki is not recommended as it was written for more general non-application-specific caching. As a result, it is only applicable when a LiteSpeed cache plugin for your application is not available or you have very customized code base application.
  
 A general way to enable LiteSpeed Cache through rewrite rules is to designate a signature token (such as a cookie) in any page, in 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 [[https://​www.litespeedtech.com/​support/​forum/​showthread.php?​t=4679| this thread on our forum]] 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 unexpected behaviors. A general way to enable LiteSpeed Cache through rewrite rules is to designate a signature token (such as a cookie) in any page, in 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 [[https://​www.litespeedtech.com/​support/​forum/​showthread.php?​t=4679| this thread on our forum]] 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 unexpected behaviors.
Line 49: Line 49:
  
 For different hosting environments,​ the steps to set up server level cache root are different. Please choose your hosting environment: ​ For different hosting environments,​ the steps to set up server level cache root are different. Please choose your hosting environment: ​
-  ​* **[[litespeed_wiki:​cache:​common_installation:​cpanel-shared|cPanel/​WHM]]** +  * [[litespeed_wiki:​cache:​common_installation:​cpanel-shared|cPanel/​WHM]] 
-  ​* **[[litespeed_wiki:​cache:​common_installation:​plesk-shared|Plesk]]** +  * [[litespeed_wiki:​cache:​common_installation:​plesk-shared|Plesk]] 
-  ​* **[[litespeed_wiki:​cache:​common_installation:​apache-config-shared|Apache Config (No Control Panel)]]** +  * [[litespeed_wiki:​cache:​common_installation:​apache-config-shared|Apache Config (No Control Panel)]] 
-  ​* **[[litespeed_wiki:​cache:​common_installation:​litespeed-shared|LiteSpeed Native]]**+  * [[litespeed_wiki:​cache:​common_installation:​litespeed-shared|LiteSpeed Native]]
  
-For Advanced Cache Configuration info, **[[litespeed_wiki:​cache:​common_installation:​advanced|look here]]**.+For Advanced Cache Configuration info, [[litespeed_wiki:​cache:​common_installation:​advanced|look here]].
 ===== Set cache policy at LSWS Server level ===== ===== Set cache policy at LSWS Server level =====
  
Line 94: Line 94:
 **"​CacheEnable public /"** means enabling cache for all URLs of this virtual host, similiar to virtual host level of global enabling. **"​CacheEnable public /"** means enabling cache for all URLs of this virtual host, similiar to virtual host level of global enabling.
  
-**"CacheDiable ​public /"** means disabling cache for all URLs of this virtual host, similiar to virtual host level of global disabling.+**"CacheDisable ​public /"** means disabling cache for all URLs of this virtual host, similiar to virtual host level of global disabling.
  
 **"​CacheLookup"​** will tell the application whether it should look up the cache or not. When "​CacheEnable"​ or "​CacheDisable"​ is used, no "​CacheLookup"​ required since it has been enabled or disabled by default. **"​CacheLookup"​** will tell the application whether it should look up the cache or not. When "​CacheEnable"​ or "​CacheDisable"​ is used, no "​CacheLookup"​ required since it has been enabled or disabled by default.
Line 100: Line 100:
 ==== CacheEnable directive ==== ==== CacheEnable directive ====
  
- *To enable ​puglic ​cache, you can add the following to .htaccess.+ *To enable ​public ​cache, you can add the following to .htaccess.
  
   <​IfModule LiteSpeed> ​   <​IfModule LiteSpeed> ​
Line 237: Line 237:
 **Note3:** The 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. **Note3:** The 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.
  
-====== Deleting Outdated Cache Files Using A Cron Job ======+**Note4:** Will I see "​Cache-Control:​max-age"​ header"​ when I use lscache? No. Please be aware there are two different concepts: ''​Cache-Control:​max-age''​ is browser cache header, not cache from the server side. While ''​x-litespeed-cache-control:​ public,​max-age=86400''​ is lscache control header, which will be seen when cache plugin being used. When using rewrite rules ''​[E=cache-control:​max-age=120]''​to enable cache as instructed in this wiki, you won't see ''​x-litespeed-cache-control''​ at the time of this writing. 
 + 
 +====== Cache Purge ====== 
 +LSWS will now delete expired cache files automatically,​ making this step optional. Assuming you still have such need to manually delete the outdated files, you can choose one of the following ways:  
 + 
 +===== Deleting Outdated Cache Files through Linux command ​Using A Cron Job =====
  
 **Note:** LSWS will now delete expired cache files automatically,​ making this step optional. A cron job can still be set up to help ensure that the cache works as expected, clearing out pages that may have been misconfigured to have a very long TTL. **Note:** LSWS will now delete expired cache files automatically,​ making this step optional. A cron job can still be set up to help ensure that the cache works as expected, clearing out pages that may have been misconfigured to have a very long TTL.
Line 243: Line 248:
 A cron job should be set to clear out old cache files that are past the set **Time To Live (TTL)**. ​ A cron job should be set to clear out old cache files that are past the set **Time To Live (TTL)**. ​
  
-To do this, you should run the crontab either as the root user or as the cache owner for self management.+To do this, you should run the crontab either as the root user or as the cache owner for self-management.
  
 <​code>​crontab -e</​code>​ <​code>​crontab -e</​code>​
Line 252: Line 257:
  
 **Note:** This cron job deletes cached files that are more than 8 minutes old every 10 minutes. Since the cache TTL is set at 120 seconds (2 minutes), it is safe to delete these files as they are way past their TTL. **Note:** This cron job deletes cached files that are more than 8 minutes old every 10 minutes. Since the cache TTL is set at 120 seconds (2 minutes), it is safe to delete these files as they are way past their TTL.
 +
 +===== Deleting Outdated Cache Files through cleancache.sh script =====
 +You can run the following command to remove old files:
 +  /​usr/​local/​lsws/​admin/​misc/​cleancache.sh /​path/​to/​cache/​directory/​
 +
 +The script basically deletes all cache files created(modified) 24 hours ago.
 +You can also run a cron job to do so. LiteSpeed Web Server [[litespeed_wiki:​cache:​cache-storage|periodically cleans up the cache storage directory]],​ so you may not find it necessary to do this yourself.
 +
 +===== How to purge cache through PHP script =====
 +Please refer to:
 +https://​www.litespeedtech.com/​support/​wiki/​doku.php/​litespeed_wiki:​cache:​no-plugin-advanced:​purge
 +
 +===== How to purge cache by URL =====
 +There is a script built into LSWS which will allow you to purge the cache by URL. It is located at ''/​usr/​local/​lsws/​admin/​misc/​purge_cache_by_url''​
 +
 +<​code>​
 + ​Usage:​ purge_cache_byurl -(r|p) [-ip <ip>] <URL>
 +
 +Purge/​Refresh cache for specific URL.
 +
 +Required Arguments:
 + ​-r|-p, ​   Refreshes/​Purges the cache for specific page.
 + ​*, ​       Full URL of the specific page to refresh/​purge.
 +
 +Optional Arguments:
 + ​-ip, ​     IP to resolve the hostname of the request to (Only
 +           if it differs from what the DNS A record is set to.)
 +
 +Notes:
 +  If running on a server other than the one the site is located on,
 +  make sure to add this machines IP to the trusted list inside of
 +  LiteSpeed Web Server or else the Purge/​Refresh requests will not
 +  work.
 +</​code>​
 +
 +**Example 1:**
 +<​code>​
 +/​usr/​local/​lsws/​admin/​misc/​purge_cache_by_url -r mywebsite.com
 +</​code>​
 +
 +**Example 2:**
 +
 +<​code>​
 +/​usr/​local/​lsws/​admin/​misc>​./​purge_cache_by_url -p www.domain.com ​         ​
 +HTTP/1.0 200 Purged
 +Date: Wed, 03 Jun 2015 05:48:31 GMT
 +Server: LiteSpeed
 +Connection: close
 +</​code>​
 +
 +**Note 1:** Required URL has to be a specific URL and can not include wildcards, otherwise it may return "400 - Bad request error"​. Alternately,​ if you want to delete all cached files, you can do it through OS command, e.g.: ''#​rm -rf /​lsws-cache-folder*/''​. ​
 +
 +**Note2:​** ​ Server_ip can not be omitted in some cases!
 +
  
 ====== Examples ====== ====== Examples ======
Line 260: Line 319:
  
 ==== Rewrite Examples for applications ==== ==== Rewrite Examples for applications ====
-If you are running Joomla site, see [[litespeed_wiki:​cache:​lscache:​joomla#​website_level_settings|examples here]]. 
- 
 If you are running vBulletin site, see [[https://​blog.litespeedtech.com/​2011/​01/​28/​speed-up-vbulletin-sites-through-litespeed-built-in-cache/​|examples here]]. If you are running vBulletin site, see [[https://​blog.litespeedtech.com/​2011/​01/​28/​speed-up-vbulletin-sites-through-litespeed-built-in-cache/​|examples here]].
- 
-If you are running Mediawiki site, see [[https://​www.litespeedtech.com/​support/​forum/​threads/​litespeed-cache-mediawiki.4747/​|examples here]]. 
  
 If you are running IP Board site, see [[litespeed_wiki:​cache:​lscache:​ip-board|examples here]]. If you are running IP Board site, see [[litespeed_wiki:​cache:​lscache:​ip-board|examples here]].
Line 356: Line 411:
   <​IfModule LiteSpeed>​   <​IfModule LiteSpeed>​
    ​CacheDisable private /    ​CacheDisable private /
 +  </​IfModule>​
 +  ​
 +
 +==== Enable cache for mobile view ====  ​
 +Setup difference cache for mobile view through cache vary. Also exclude some folders from cache, then cache everything else 
 +
 +
 +  <​IfModule LiteSpeed>​
 +  RewriteEngine On
 +  CacheDisable public /
 +  RewriteCond %{HTTP_USER_AGENT} "​iPhone|iPod|BlackBerry|Palm|Mobile|Opera Mini|Fennec|Windows Phone"
 +  RewriteRule .* - [E=Cache-Control:​vary=ismobile]
 +  RewriteCond %{REQUEST_METHOD} ^HEAD|PURGE|GET$
 +  RewriteCond %{ORG_REQ_URI} !/news
 +  RewriteCond %{ORG_REQ_URI} !/admincp
 +  RewriteRule .* - [E=Cache-Control:​max-age=120]
   </​IfModule>​   </​IfModule>​
  • Admin
  • Last modified: 2020/09/23 20:42
  • by Lisa Clarke