Configure the LSCache Module¶
OpenLiteSpeed offers built-in page caching to greatly improve page load times.
What is LiteSpeed Cache ?¶
LiteSpeed Cache (LSCache) is a built-in, high-performance dynamic content acceleration feature of the LiteSpeed Web Server.
LSCache's features are very similar to those in Apache's mod_cache
, but implemented in a more efficient way. LiteSpeed cache works like Varnish. It is an output cache for dynamic content, 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 content. The unique feature of LiteSpeed Cache is that it uses rewrite rules (either in configuration files or .htaccess
) to control its behavior for maximum flexibility.
Configure LiteSpeed Cache¶
You can find the server-level settings in the WebAdmin Console under Server Configuration > Modules > Cache or in the configuration file located at /usr/local/lsws/conf/httpd_config.conf
.
Cache Module Parameters¶
- enableCache: This setting enables or disables public cache. (Set to
1
to enable. Set to0
to disable.) If both public and private cache are enabled, OpenLiteSpeed will save to the private cache first. - qsCache: This setting enables or disables caching of URIs with query strings. (Set to
1
to enable. Set to0
to disable.) - reqCookieCache: This setting tells the cache how to react to requests with cookies. If enabled (set to
1
), requests with cookies will be served a response from cache if a cached copy exists. If disabled (set to0
), requests with cookies, will not be served a response from cache (even if a cached copy of the response exists). - respCookieCache: This setting tells the cache how to treat responses with a
Set-Cookie
header. If enabled (set to1
), responses with aSet-Cookie
header will be cached. If disabled (set to0
), responses with aSet-Cookie
header will not be cached. - ignoreReqCacheCtrl: Enabling this setting (set to
1
) will tell LiteSpeed's cache to ignore any cache control settings in the request. - ignoreRespCacheCtrl: Enabling this setting (set to
1
) will tell LiteSpeed's cache to ignore any cache control settings in the response. - expireInSeconds: This setting sets the expiration time (in seconds) for cached resources.
- maxStaleAge: This setting sets the maximum time (in seconds) that the cache can serve stale cache. (Stale cache refers to the serving of expired cached resources when a newer resource is not yet available.)
- enablePrivateCache: This setting enables or disables private cache. (Set to
1
to enable. Set to0
to disable.) If both public and private cache are enabled, OpenLiteSpeed will save to the private cache first. - privateExpireInSeconds: This setting sets the expiration time (in seconds) for cached resources in a private cache.
- storagePath: This setting sets the directory where cache data will be stored. Paths starting with a
/
will use an absolute path. Paths without the starting/
will be relative to the OpenLiteSpeed root directory. The variables$VH_ROOT
,$VH_NAME
and$SERVER_ROOT
can be used to designate separate caches for different virtual hosts. If this parameter is not explicitly configured, cache will be stored in acachedata
directory under the OpenLiteSpeed root. For example,storagepath cachedata
orstoragepath /usr/local/lsws/cachedata
orstoragepath $SERVER_ROOT/cachedata
- checkPrivateCache: Enabling this setting (set to
1
) will tell LiteSpeed's cache to check the private cache for entries to serve from the cache. - checkPublicCache: Enabling this setting (set to
1
) will tell LiteSpeed's cache to check the public cache for entries to serve from the cache.
Note
The enableCache and enablePrivateCache settings are not available at the server level, as it is strongly recommended that you not enable caching server-wide. If you have a need for global caching, you may configure a Context for that purpose.
You can change settings from the WebAdmin Console interface, or you can edit the /usr/local/lsws/conf/httpd_config.conf
config file.
This is a sample configuration that you can copy and paste into the config file:
module cache {
internal 1
ls_enabled 1
storagePath $SERVER_ROOT/cachedata
checkPrivateCache 1
checkPublicCache 1
maxCacheObjSize 10000000
maxStaleAge 0
qsCache 1
reqCookieCache 1
respCookieCache 1
ignoreReqCacheCtrl 1
ignoreRespCacheCtrl 0
enableCache 0
expireInSeconds 3600
enablePrivateCache 0
privateExpireInSeconds 3600
}
Cache Settings Inheritance¶
All modules must be registered at the server level. Once a module is registered, it's settings are inherited by more specific setting levels (virtual host, context, and script handler). Configurations can be modified at these more specific levels. Settings at more specific levels always override more general levels. For example, virtual host-level configurations override server-level configurations and context-level configurations override virtual host-level configurations.
Set up Virtual Host-Level Cache Settings¶
If you run a shared hosting server, you may wish to set up vhost-level settings for each individual user's root. If no vhost-level configuration is set, the server-level configuration will be inherited.
To set up vhost-level caching, add the cache module under each virtual host, and edit the settings there in the same way that you did at the server level, like so:
Navigate to Example Virtual Host > Modules > Add, and select cache
from the module drop down list. Enter your desired Module Parameters for cache settings, and set Enable Module to Yes
.
You can change any setting that you wish, but the most important virtual host cache setting should be storagePath $VH_ROOT/lscache
, which will set the path to be different than the server level. Each user will have the ability to purge cache from their own account. This is a sample configuration that you may copy and paste:
module cache {
ls_enabled 1
storagePath $VH_ROOT/lscache
checkPrivateCache 1
checkPublicCache 1
maxCacheObjSize 10000000
maxStaleAge 0
qsCache 1
reqCookieCache 1
respCookieCache 1
ignoreReqCacheCtrl 1
ignoreRespCacheCtrl 0
enableCache 0
expireInSeconds 3600
enablePrivateCache 0
privateExpireInSeconds 3600
}
Using LSCache¶
Configuring the cache module at the server level is only one part of the equation. You still need to enable caching for your web apps, and this can be done by installing the corresponding LSCache plugin, or using rewrite rules in .htaccess
if no plugin is available.
Test¶
There's a simple way to see if a URL is cached by LiteSpeed: the LSCache Check Tool.
Enter the URL you wish to check, and the tool will respond with an easy-to-read Yes or No result, and a display of the URL's response headers, in case you want to examine the results more closely.
In addition to LSCache support, the tool can detect cache hits, and can detect when sites are using LiteSpeed Web ADC or QUIC.cloud CDN for caching.
Additionally, a Stale Cache Warning will alert you if browser cache is detected on dynamic pages. This is because browser cache may interfere with the delivery of fresh content.
Learn More¶
There is a lot more to know, if you want to use LiteSpeed Cache to its greatest potential. Please see the LiteSpeed Cache documentation for a deeper dive.