Help:Modules:Cache

From OpenLiteSpeed
Jump to: navigation, search

This wiki will go over adding and configuring OpenLiteSpeed's cache module. Using this module, OpenLiteSpeed offers page caching to greatly improve page load times.

For this guide, we assume you already have a working installation of OpenLiteSpeed 1.3.1 or higher. Module support is only available in versions 1.3 or higher. Starting with version 1.3.1, the caching module is automatically made, installed, and has the cache.so dynamic library added to the OpenLiteSpeed modules directory. If you do not find the cache.so file in the modules directory, you may install and register the module manually. These steps are covered in the guide to registering a module.

Contents

Registering the module

Download and install OpenLiteSpeed 1.3 or higher if you have not done so

Add the cache module by going to WebAdmin console > Configuration > Server > Modules > Add

Cache Mod Add.png

Enter the name of the module as "cache"

Cache Mod Name.png

Copy in the parameters

These settings will control cache performance. Below are some default settings. Configuring these settings will be covered in the Configuration section below.

enableCache 0
qsCache 1
reqCookieCache 1
respCookieCache 1
ignoreReqCacheCtrl 0
ignoreRespCacheCtrl 0
expireInSeconds 2000
maxStaleAge 1000
enablePrivateCache 1
privateExpireInSeconds 1000
storagePath

Note: These parameters are deliberately conservative. See the parameters section below for details on configuration.

Cache Mod Param.png

Save the module settings

Cache Mod Save.png

Further configure the module at different levels

The module can be further configured at the virtual host, context, and script handler levels. More specific levels of configuration will always override more general levels. For example, virtual host-level configurations override server-level configurations and context-level configurations override virtual host-level configurations. Configurations are covered in the Configuration section below.

Test

View headers on cacheable pages. A page served from public cache will display the header X-LiteSpeed-Cache: hit. A page served from private cache will display the header X-LiteSpeed-Cache: hit, private.

Priv Cache Header.png

Configuration

Parameters in the WebAdmin Console

The OpenLiteSpeed cache module parameters mirror the cache settings in Enterprise. These parameters provide basic ways of setting up the cache.

  • enableCache = This setting enables or disables public cache. (Set to "1" to enable. Set to "0" to disable.) If both public and private cache are enabled, OpenLiteSpeed will save to and serve from private cache first.
  • qsCache = This setting enables or disables caching of URIs with query strings. (Set to "1" to enable. Set to "0" 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 to "0"), 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 to "1"), responses with a Set-Cookie header will be cached. If disabled (set to "0"), responses with a Set-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 to "0" to disable.) If both public and private cache are enabled, OpenLiteSpeed will save to and serve from 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. When left blank, cache will be stored in a cachedata directory under the OpenLiteSpeed root.

Configuring using rewrite rules

Rewrite rules are a much more powerful way to configure the cache. Configuring the OpenLiteSpeed cache with rewrite rules follows the syntax laid out for LSWS Enterprise's LSCache — this basically means the Apache mod_rewrite syntax with one new, LiteSpeed-specific environment variable for controlling the cache: E=cache-control: (Use of this environment variable will be clear in the examples listed below.)

Rewrite rules for configuring cache can be added in the rewrite rule configurations of virtual hosts or contexts (WebAdmin console > Configuration > Virtual Hosts > your virtual host > Rewrite or Context). When adding rewrite rules, you must be sure to turn on the "Enable Rewrite" setting. Also remember, as noted in the #Inheritance section, virtual host- or context-level configurations will override server-level configurations, including whether the cache is enabled or not.

Here are some examples for cache setups using rewrite rules:

Cache everything from a certain directory

This rewrite rule causes any response from the cacheable directory with the suffix .php to be cached for two minutes.

 RewriteRule cacheable/(.*\.php)?$ - [L,E=cache-control:max-age=120]

Cache all resources with certain suffixes

This set of rewrite conditions and rules causes requests with the suffixes .php, .html, .htm, .feed, .pdf, and .raw to be served responses from the private cache. Note that there are also additional requirements, such that the requests be GET or HEAD requests with the cookie loginuser and that the original request (before any other rewrites were applied does not start with /index.php.

 # this part is for private cache, note that HTTP_COOKIE is for loginuser
 RewriteCond %{REQUEST_METHOD} ^HEAD|GET$
 RewriteCond %{HTTP_COOKIE} loginuser
 RewriteCond %{ORG_REQ_URI} !^/index.php$
 RewriteCond %{ORG_REQ_URI} (\.php|\.html|\.htm|\.feed|\.pdf|\.raw|/[^.]*)$  [NC]
 RewriteRule .* - [E=cache-control:private,L]

Turn off caching for this virtual host or context

 RewriteRule .* - [E=Cache-Control:no-cache]

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.