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 [2017/04/20 23:08]
Michael Alegre [1. Create Cache]
litespeed_wiki:cache:no-plugin-advanced:purge [2019/07/02 13:50]
Lisa Clarke [Procedures]
Line 1: Line 1:
-====== LSCache ​PURGE ======+====== ​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.
  
-===== Summary ===== +If there is no plugin for your applicationand you are using LiteSpeed Cache [[litespeed_wiki:​cache:​no-plugin-setup-guidline|with rewrite rules]], then you may find purging ​to be a challenge.
-As of 4.1.12LSWS supports ​cache PURGE operation ​to accommodate the demand on cache manipulation.+
  
-===== Procedures ===== +This is how LSCache works when you are using it without a plugin
-Here are the steps to PURGE cache:+
  
 +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, a cache PURGE operation is supported** to accommodate cache manipulation on demand.
  
 +===== Procedures =====
  
 +Let's assume you've set up rewrite rules like the following in .htaccess:
 +<​code> ​ <​IfModule LiteSpeed>​
 +  RewriteEngine On
 +  RewriteCond %{REQUEST_METHOD} ^HEAD|GET$
 +  RewriteRule .* - [E=Cache-Control:​max-age=120]
 +  </​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 ruleslike so
-==== Create a wrapper script purge.php for PURGE ==== +<​code><​IfModule LiteSpeed>
- +
-<​code>​ +
-<?php +
-$fp = fsockopen('​192.168.0.81'​80, $errno, $errstr, 2); +
-if (!$fp) { +
-    echo "​$errstr ($errno)\n";​ +
-} else { +
-    $out = "PURGE /hello.php HTTP/​1.0\r\n"​ +
-    . "Hostwww.example.com\r\n"​ +
-    //. "​Authorization:​ Basic "​.base64_encode("​userid:​password"​)."​\r\n"​ +
-    . "​Connection:​ Close\r\n\r\n";​ +
-    fwrite($fp, $out); +
-    echo $out; +
-    while (!feof($fp)) { +
-        echo fgets($fp, 128); +
-    } +
-    fclose($fp);​ +
-+
-?> +
-</code> +
- +
-**Note:​** ​  +
-  - We are using internal IP address 192.168.0.80 for illustration purposes only. In a real world application,​ make sure that vhost listens on 192.168.0.80 for domain name ''​<nowiki>http://​example.com</​nowiki>''​. +
-  - Authentication can be added to make the PURGE more secure. Comments in the script simplify the process. +
- +
- +
-==== Set up Rewrite Rules to Allow PURGE ==== +
- +
-put the following rules in .htaccess +
   RewriteEngine On   RewriteEngine On
   RewriteCond %{REQUEST_METHOD} ^HEAD|GET|PURGE$   RewriteCond %{REQUEST_METHOD} ^HEAD|GET|PURGE$
-  ​RewriteCond %{REQUEST_URI} !^/​purge\.php$ +  RewriteRule .* - [E=Cache-Control:​max-age=120] 
-  ​RewriteRule .* - [E=Cache-Control:​max-age=120,L]+  </​IfModule></​code>  ​
  
-**Note:** PURGE needs to be in the allowed method.+The cache purge can be initiated through ​the following curl command: 
 +<​code>​curl -i -X PURGE http://​example.com/</​code>​
  
-=== Whitelist source IP to issue PURGE request ====+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>​
  
-Admin Console ​=> Configuration ​=> Server ​=> Security ​=> Allow List+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''​) .
  
-append Source_IP with tailing '​T'​ for trusted IP like  'ALL, 192.168.0.81T'​+===== 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/​
  
 +HTTP/1.1 200 Purged
 +Content-Length:​ 0
 +Date: Fri, 28 Jun 2019 18:26:46 GMT
 +Server: LiteSpeed
 +Alt-Svc: quic=":​443";​ ma=2592000; v="​35,​39,​43,​44"​
 +Connection: close
 +</​code>​
  
-===== Operations ===== +Check the header of ''<​nowiki>​http://​example.com/</​nowiki>'' ​againand it should show ''​X-Litespeed-Cache: ​miss''​ instead of ''​X-Litespeed-Cache:​ hit''​. ​This means the cache was purged ​successfullyIf you hit the URL the second ​time, you should see ''​X-Litespeed-Cache:​ hit'' again.
- +
- +
-==== 1. Create Cache ==== +
- +
- ​1.1 ​ point browser to ''<​nowiki>​http://​www.example.com/​hello.php</​nowiki>''​ +
- +
- ​1.2 ​ cache storage has a new file created for hello.php (for illustration purposeswe cleaned cache storage in our lab prior to step 1). +
-<​file>​ +
-[root@cptest diskcache]# ls -lrt */*/* +
-9/c/6: +
-total 12 +
--rw------- 1 nobody nobody 11638 May  2 15:42 9c6eae83978ecd87 +
- +
-[root@cptest diskcache]#​ +
-</​file>​ +
- +
-==== 2. Request the same resource from cache ==== +
- +
-  request http://​www.example.com/​hello.php again from browser. receive ​'​X-Litespeed-Cache: ​hit' ​response header as a prove  +
-<​file>​ +
-   Date: Wed, 02 May 2012 23:59:53 GMT +
-   ​Content-Encoding:​ gzip +
-   X-Litespeed-Cache:​ hit   <=== here it is +
-   ​Connection:​ Keep-Alive +
-   ​Content-Length:​ 10 +
-   ​Server:​ LiteSpeed +
-   Vary: Accept-Encoding +
-   ​Content-Type:​ text/html +
-   ​Keep-Alive:​ timeout=5, max=100 +
-</​file>​ +
- +
- +
- +
-==== 3. Issue purge request from server ==== +
- +
- 3.1 1st time, '200 Purged' ​status returned. +
-<​file>​ +
-   ​[test@test ~]$ php purge.php +
-   PURGE /hello.php HTTP/1.0 +
-   Host: www.example.com +
-   ​Connection:​ Close +
- +
-   ​HTTP/​1.0 200 Purged +
-   Date: Wed, 02 May 2012 23:48:23 GMT +
-   ​Server:​ LiteSpeed +
-   ​Connection:​ close +
- +
-   ​[test@test ~]$  +
-</​file>​ +
- +
-**Note:** '​HTTP/​1.0 200 Purged'​ indicates ​the resource is purged. +
- +
-Rewrite Log shown  +
-<​file>​ +
-2012-05-02 15:​43:​20.280 [INFO] [192.168.0.81:​60174-0#​APVH_lstest.com] [REWRITE] strip base: '/'​ from URI: '/​phpinfo.php'​ +
-2012-05-02 15:​43:​20.280 [INFO] [192.168.0.81:​60174-0#​APVH_lstest.com] [REWRITE] Rule: Match '​phpinfo.php'​ with pattern '​.*',​ result:  +
-+
-2012-05-02 15:​43:​20.280 [INFO] [192.168.0.81:​60174-0#​APVH_lstest.com] [REWRITE] Cond: Match '​PURGE'​ with pattern '​^HEAD|GET|PURGE$',​  +
-result: 1 +
-2012-05-02 15:​43:​20.280 [INFO] [192.168.0.81:​60174-0#​APVH_lstest.com] [REWRITE] Cond: Match '/​phpinfo.php'​ with pattern '​^/​purge\.php +
-$', result: -1 +
-2012-05-02 15:​43:​20.280 [INFO] [192.168.0.81:​60174-0#​APVH_lstest.com] [REWRITE] apply cache-control: '​max-age=120'​. +
-2012-05-02 15:​43:​20.280 [INFO] [192.168.0.81:​60174-0#​APVH_lstest.com] [REWRITE] No substition +
-2012-05-02 15:​43:​20.280 [INFO] [192.168.0.81:​60174-0#​APVH_lstest.com] [REWRITE] Last Rule, stop! +
-2012-05-02 15:​43:​20.281 [NOTICE] [192.168.0.81:​60174-0#​APVH_lstest.com:​cache] [PUBLIC CACHE] PURGE /​phpinfo.php,​ result: 1 +
-2012-05-02 15:​43:​20.281 [DEBUG] [192.168.0.81:​60174-0#​APVH_lstest.com:​cache] HttpConnection::​sendHttpError(),​code=200 Purged +
- +
- ... +
- +
-</​file>​ +
- +
- 3.2 2nd purge request results in 405 error +
- +
-<​file>​ +
-   ​[test@test ~]$ php purge.php +
-   PURGE /hello.php HTTP/1.0 +
-   Host: www.lstest.com +
-   ​Connection:​ Close +
- +
-   ​HTTP/​1.0 405 Method Not Allowed +
-   Date: Wed, 02 May 2012 23:37:44 GMT +
-   ​Server:​ LiteSpeed +
-   ​Connection:​ close +
-   ​Cache-Control:​ private, no-cache, max-age=0 +
-   ​Pragma:​ no-cache +
-   ​Content-Type:​ text/html +
-   ​Content-Length:​ 383 +
- +
-   ​[test@test ~]$ +
-</​file>​ +
- +
-Rewrite Log shown +
-<​file>​ +
-2012-05-02 15:​43:​39.588 [NOTICE] [192.168.0.81:​60175-0#​APVH_lstest.com] Content len: 0, Request line: 'PURGE /​phpinfo.php HTTP/​1.1'​ +
-2012-05-02 15:​43:​39.588 [DEBUG] [192.168.0.81:​60175-0#​APVH_lstest.com] Find context with URI: [/], location: [/​home/​lstestc/​public_html/​] +
-... +
-2012-05-02 15:​43:​39.588 [INFO] [192.168.0.81:​60175-0#​APVH_lstest.com] [REWRITE] strip base: '/'​ from URI: '/​phpinfo.php'​ +
-2012-05-02 15:​43:​39.588 [INFO] [192.168.0.81:​60175-0#​APVH_lstest.com] [REWRITE] Rule: Match '​phpinfo.php'​ with pattern '​.*',​ result: 1 +
-2012-05-02 15:​43:​39.588 [INFO] [192.168.0.81:​60175-0#​APVH_lstest.com] [REWRITE] Cond: Match '​PURGE'​ with pattern '​^HEAD|GET|PURGE$',​ result: 1 +
-2012-05-02 15:​43:​39.588 [INFO] [192.168.0.81:​60175-0#​APVH_lstest.com] [REWRITE] Cond: Match '/​phpinfo.php'​ with pattern '​^/​purge\.php$',​ result: -1 +
-2012-05-02 15:​43:​39.588 [INFO] [192.168.0.81:​60175-0#​APVH_lstest.com] [REWRITE] apply cache-control:​ '​max-age=120'​. +
-2012-05-02 15:​43:​39.588 [INFO] [192.168.0.81:​60175-0#​APVH_lstest.com] [REWRITE] No substition +
-2012-05-02 15:​43:​39.588 [INFO] [192.168.0.81:​60175-0#​APVH_lstest.com] [REWRITE] Last Rule, stop! +
-2012-05-02 15:​43:​39.588 [DEBUG] [192.168.0.81:​60175-0#​APVH_lstest.com] HttpConnection::​sendHttpError(),​code=405 Method Not Allowed +
-2012-05-02 15:​43:​39.588 [DEBUG] [192.168.0.81:​60175-0#​APVH_lstest.com] redirect to:  +
-        URI=[/​405.shtml],​ +
-        QueryString=[] +
-</​file>​ +
- +
-==== 4. Verify cache is purged ​==== +
- +
- 4.1 check cache file -- gone +
-<​file>​ +
-   ​[test@test ~]$ ls -lrt */*/* +
- +
-   ​9/​c/​6:​ +
-   total 0 +
-   ​[test@test ~]$  +
-</​file>​ +
- +
- 4.2 request resource 3rd time from browser*no* '​X-Litespeed-Cache:​ hit' ​response header received. +
-<​file>​ +
-   Date: Wed, 02 May 2012 23:42:18 GMT +
-   ​Content-Encoding:​ gzip +
-   ​Transfer-Encoding:​ Identity +
-   ​Connection:​ close +
-   ​Server:​ LiteSpeed +
-   Vary: Accept-Encoding +
-   ​Content-Type:​ text/html +
-</​file>​+
  • Admin
  • Last modified: 2020/09/23 20:44
  • by Lisa Clarke