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-advanced:purge [2019/06/28 18:38]
Jackson Zhang [testing]
litespeed_wiki:cache:no-plugin-advanced:purge [2019/07/02 13:50]
Lisa Clarke [Procedures]
Line 1: Line 1:
-====== LSCache ​PURGE for no plugin rewrite rules based lscache solution======+====== ​Purging ​LSCache ​Without a Plugin ​====== 
 +If you are using a LiteSpeed Cache plugin, then you should not have to worry about manually purging the cache when content changes on your site. The plugin is meant to handle such situations automatically. Additionally,​ there is usually a "​Purge"​ button available if manual management is required. We strongly recommend using a plugin if it is available. Check our [[litespeed_wiki:​cache#​lscache_plugins_by_application|List of LSCache plugins]] to see if one is available for your application.
  
-Normally the user won't need to worry about cache purge when LiteSpeed cache plugin installed and LiteSpeed cache plugin will do the purge for you automatically whenever this is any change on the website. ​ However, for some applications,​ specially customized applications,​ there are no LSCache ​plugin ​available yet(check this [[litespeed_wiki:​cache#​lscache_plugins_by_application|list]] to see if the lscache plugin available ​for your application ​or not) then you may consider ​[[litespeed_wiki:​cache:​no-plugin-setup-guidline|rewrite rules based cache solutions]].  The logic behind the rewrite rules based lscache is to cache everything excluding some URLs, admin URLS or some certain conditions to a short period of time (for instance 2 mins), then the cache will be expired and pages will need to be re-cached again. If there is any change occurring before the cache expiration time, cache won't be purged and the changes will need to wait cache expired to refresh the update. ​ this kind of updating delay could be fine for most of the case when cache expiration TTL is set to very short time. However, in some situation, such as the TTL is as long as 24 hours or update needs to be refreshed immediately,​ cache purge will be needed for such cases.+If there is no plugin for your application, and you are using LiteSpeed Cache [[litespeed_wiki:​cache:​no-plugin-setup-guidline|with rewrite rules]], then you may find purging ​to be a challenge.
  
-Starting from LSWS version 4.1.12, ​LSWS supports ​cache PURGE operation to accommodate ​the demand on cache manipulation. The following wiki will explain how to do cache purge when rewrite rule based lscache solution used.+This is how LSCache works when you are using it without a plugin:  
 + 
 +Everything is cached. Some URLs or particular conditions may be excluded, but by default everything is cached for a short period of time (for instance, two minutes). After that time, the cache expires and pages will need to be re-cached again. If any content changes befor the cache expires, the page will not be purged. Changes will not be reflected in the frontend until the cache expires naturally. 
 + 
 +This kind of delay is fine for many cases, particularly if the cache expiration TTL is set to a very short time. However, if your TTL is very long, or if an urgent update cannot wait for the cache to expire, a mechanism for purging the cache is necessary. **As of LiteSpeed Web Server v4.1.12, ​cache PURGE operation ​is supported** ​to accommodate cache manipulation ​on demand.
  
 ===== Procedures ===== ===== Procedures =====
  
-  - Assuming the user has setup rewrite rules like the following in .htaccess<​code> ​ <​IfModule LiteSpeed>​+Let's assume you've set up rewrite rules like the following in .htaccess
 +<​code> ​ <​IfModule LiteSpeed>​
   RewriteEngine On   RewriteEngine On
   RewriteCond %{REQUEST_METHOD} ^HEAD|GET$   RewriteCond %{REQUEST_METHOD} ^HEAD|GET$
   RewriteRule .* - [E=Cache-Control:​max-age=120]   RewriteRule .* - [E=Cache-Control:​max-age=120]
-  </​IfModule></​code> ​The rules are to enable cache for everything for two minutes. ​ The RewriteCon ​for %{REQUEST_METHOD} is optionalbut if the user does add it, ''​PURGE'' ​method will need to be added as well to make PURGE workThe rewrite rules will be changed to<​code><​IfModule LiteSpeed>​+  </​IfModule></​code> ​ 
 +   
 +These rules enable cache for everything for two minutes. The ''​RewriteCond'' ​for ''​%{REQUEST_METHOD}'' ​is optional ​in general, ​but if do use it, and you want the ability to purge the cache, you will need to add ''​PURGE''​ to it 
 + 
 +Change the rewrite rules, like so: 
 +<​code><​IfModule LiteSpeed>​
   RewriteEngine On   RewriteEngine On
   RewriteCond %{REQUEST_METHOD} ^HEAD|GET|PURGE$   RewriteCond %{REQUEST_METHOD} ^HEAD|GET|PURGE$
   RewriteRule .* - [E=Cache-Control:​max-age=120]   RewriteRule .* - [E=Cache-Control:​max-age=120]
   </​IfModule></​code>  ​   </​IfModule></​code>  ​
-  - The cache purge can be initiated through the following curl command <​code>​curl -i -X PURGE http://​example.com/</​code>​ Please note that if the hopepage http://​example.com/​index.php needed to be purged, the trailling ''/''​ is required and the follwing command won't work<​code>​curl -i -X PURGE http://​example.com</​code>​ 
-  - The above curl command can be run either within the server or from a remote server. If purge curl command will be run from a remote server, the remote server IP will need to be added to LiteSpeed webserver trusted IP (LSWS Admin Console => Configuration => Server => Security => Allow List, append Source_IP with tailing '​T'​ for trusted IP like  'ALL, x.x.x.xT',​ ) 
  
-===== testing ​=====+The cache purge can be initiated through the following curl command: 
 +<​code>​curl -i -X PURGE http://​example.com/</​code>​ 
 + 
 +Please note that if the homepage ''<​nowiki>​http://​example.com/​index.php</​nowiki>''​ needs to be purged, the trailling ''/''​ is required. The follwing command won't work: 
 +<​code>​curl -i -X PURGE http://​example.com</​code>​ 
 + 
 +The above curl command can be run either within the server or from a remote server. If a purge curl command is to be run from a remote server, the remote server IP will need to be added to LiteSpeed Web Server'​s Trusted IP list. Navigate to **LSWS Admin Console > Configuration > Server > Security**, and append the source IP with a trailing ''​T''​ to **Allow List**. (The ''​T''​ signifies the IP is Trusted. It should look like ''​ALL,​ x.x.x.xT''​) . 
 + 
 +===== Testing ​===== 
 + 
 +Visit ''<​nowiki>​http://​example.com/</​nowiki>''​ through the browser and ensure ''​X-Litespeed-Cache:​ hit''​ is set. 
 +  
 +Then, using the above curl command, purge the homepage either within the server or from a remote server, like so: 
 +<​code>​curl -i -X PURGE http://​example.com/​
  
-  - visit http://​example.com/​ through the browser and ensure '​X-Litespeed-Cache:​ hit' is set.  
-  - Then using the above curl command to purge the homepage either within the server or from a remote server, ​ 
-  <​code>​ 
-curl -i -X PURGE http://​example.com/​ 
 HTTP/1.1 200 Purged HTTP/1.1 200 Purged
 Content-Length:​ 0 Content-Length:​ 0
Line 32: Line 50:
 Connection: close Connection: close
 </​code>​ </​code>​
-- Then check the header of http://​example.com again, it will show ''​X-Litespeed-Cache:​ miss''​ instead of ''​X-Litespeed-Cache:​ hit''​, which means the cache purged successfully. If you hit the URL the second time, it will be ''​X-Litespeed-Cache:​ hit''​ again.+ 
 +Check the header of ''<​nowiki>​http://​example.com/</​nowiki>'' ​again, ​and it should ​show ''​X-Litespeed-Cache:​ miss''​ instead of ''​X-Litespeed-Cache:​ hit''​. This means the cache was purged successfully. If you hit the URL the second time, you should see ''​X-Litespeed-Cache:​ hit''​ again.
  • Admin
  • Last modified: 2020/09/23 20:44
  • by Lisa Clarke