This is an old revision of the document!


LSCache PURGE for no plugin rewrite rules based lscache solution

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 list to see if the lscache plugin available for your application or not) then you may consider 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 a 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.

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.

  1. Assuming the user has setup rewrite rules like the following in .htaccess
      <IfModule LiteSpeed>
      RewriteEngine On
      RewriteCond %{REQUEST_METHOD} ^HEAD|GET$
      RewriteRule .* - [E=Cache-Control:max-age=120]
      </IfModule>

    The rules are to enable cache for everything for two minutes. The RewriteCon for %{REQUEST_METHOD} is optional. but if the user does add it, PURGE method will need to be added as well to make PURGE work. The rewrite rules will be changed to

    <IfModule LiteSpeed>
      RewriteEngine On
      RewriteCond %{REQUEST_METHOD} ^HEAD|GET|PURGE$
      RewriteRule .* - [E=Cache-Control:max-age=120]
      </IfModule>
  2. The cache purge can be initiated through the following curl command
    curl -i -X PURGE http://example.com/

    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

    curl -i -X PURGE http://example.com
  3. 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', )
  1. visit http://example.com/ through the browser and ensure 'X-Litespeed-Cache: hit' is set.
  2. Then using the above curl command to purge the homepage either within the server or from a remote server,
    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
  3. 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.
  • Admin
  • Last modified: 2019/06/28 18:38
  • by Jackson Zhang