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/09/07 20:31]
Jackson Zhang [How to purge cache by URL?]
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 236: Line 236:
  
 **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.
 +
 +**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 ====== ====== Cache Purge ======
Line 260: Line 262:
   /​usr/​local/​lsws/​admin/​misc/​cleancache.sh /​path/​to/​cache/​directory/​   /​usr/​local/​lsws/​admin/​misc/​cleancache.sh /​path/​to/​cache/​directory/​
  
- The script basically ​delete ​all cache files created(modified) 24 hours ago. +The script basically ​deletes ​all cache files created(modified) 24 hours ago. 
-You can also run a cron job to do so.+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=====+===== How to purge cache through ​PHP script =====
 Please refer to: Please refer to:
 https://​www.litespeedtech.com/​support/​wiki/​doku.php/​litespeed_wiki:​cache:​no-plugin-advanced:​purge https://​www.litespeedtech.com/​support/​wiki/​doku.php/​litespeed_wiki:​cache:​no-plugin-advanced:​purge
  
-===== How to purge cache by URL===== +===== How to purge cache by URL ===== 
- How to purge cache by URL? There is a php script built in with LSWS and is located at /​usr/​local/​lsws/​admin/​misc/​purge_cache_byurl.php+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''​
  
-You can use it like the following example: 
 <​code>​ <​code>​
-/usr/​local/​lsws/​admin/​fcgi-bin/admin_php5 ​/usr/​local/​lsws/​admin/​misc/​purge_cache_byurl.php -r mywebsite.com /index.php+ ​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>​ </​code>​
  
-The script code:+**Example 1:**
 <​code>​ <​code>​
-<?php+/​usr/​local/​lsws/​admin/​misc/​purge_cache_by_url -r mywebsite.com 
 +</code>
  
-/**** +**Example ​2:**
- * purge_cache_byurl +
- * +
- * Example: ​/​usr/​local/​lsws/​admin/​fcgi-bin/​admin_php5 /​usr/​local/​lsws/​admin/​misc/​purge_cache_byurl.php -r mywebsite.com /​index.php +
- */+
  
-if ($argc ​4 || $argc 6) { +<code
-    echo "​Invalid arguments!\n";​ +/​usr/​local/​lsws/​admin/​misc>​./​purge_cache_by_url ​-p www.domain.com          ​ 
-    echo  "​Usage:​ php $argv[0] ​-(r|pdomain ​url [server_ip] [port] +HTTP/1.0 200 Purged 
-    -r method option: Refresh cache (use stale cache while updating cache) +DateWed, 03 Jun 2015 05:48:31 GMT 
-    -p method optionPurge cache (delete cache entry) +ServerLiteSpeed 
-        domainrequired parameter for domain name  +Connectionclose 
-        urlrequired parameter for url +</code>
-        ​server_ipoptional parameter, default is 127.0.0.1 +
-        ​server_portoptional parameter, default is 80 +
-"; +
-    exit; +
-+
-if ( $argv[1] == '​-p'​ ) +
-    $method = "​PURGE";​ +
-else if ($argv[1] == '​-r'​ ) +
-    $method = "​REFRESH";​ +
-else +
-+
-    echo "​ERROR:​ unknown or missing method option";​ +
-    exit; +
-+
-$domain = $argv[2]; +
-$url = $argv[3]; +
-$server_ip = ($argc ​>= 5) ? $argv[4] : '​127.0.0.1';​ +
-$port = ($argc == 6) ? $argv[5] : 80;+
  
 +**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*/''​. ​
  
-$fp = fsockopen($server_ip,​ $port, $errno, $errstr, 2); +**Note2:**  Server_ip can not be omitted in some cases!
-if (!$fp) { +
-    echo "​$errstr ($errno)\n";​ +
-} else { +
-    $out = "​$method $url HTTP/​1.0\r\n"​ +
-        . "Host: $domain\r\n"​ +
-        . "​ConnectionClose\r\n\r\n";​ +
-    fwrite($fp, $out); +
-    while (!feof($fp)) { +
-        echo fgets($fp, 128); +
-    } +
-    fclose($fp);​ +
-}+
  
-?> 
  
-</​code>​ 
 ====== Examples ====== ====== Examples ======
  
Line 336: 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 432: 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