Blog / Others/ OpenLiteSpeed Page Cache Module Configuration and Rewrite Rules Guide

OpenLiteSpeed Page Cache Module Configuration and Rewrite Rules Guide

OpenLiteSpeed Page Cache Overview

The built-in page cache module in OpenLiteSpeed effectively reduces server load and significantly improves website page loading speed. For high-traffic websites, enabling caching is a crucial step for performance optimization.

Note: All modules must be registered at the server level before they can be configured. This article assumes you have completed module registration. If not, please refer to the relevant documentation to complete this step first.

Enabling the Page Cache Module

To use the page cache feature, ensure your OpenLiteSpeed version is 1.3 or higher. Follow these configuration steps:

Step 1: Add the Cache Module

  1. Log in to the OpenLiteSpeed WebAdmin console.
  2. Navigate to: Configuration > Server > Modules.
  3. Click the Add button to add a new module.

Step 2: Configure Module Parameters

In the module addition interface, configure the following settings:

  • Module Name: Enter cache.
  • Module Parameters: Enter the following configuration (parameter explanations are provided later):
enableCache 0
qsCache 1
reqCookieCache 1
respCookieCache 1
ignoreReqCacheCtrl 0
ignoreRespCacheCtrl 0
expireInSeconds 2000
maxStaleAge 1000
enablePrivateCache 1
privateExpireInSeconds 1000
storagePath

After configuration, click Save in the top-right corner. This is a server-wide configuration but can be overridden and refined at the virtual host level.

Configuring Cache Rewrite Rules for Virtual Hosts

After enabling the cache module at the server level, you can configure Rewrite rules for specific virtual hosts to control caching behavior.

Prerequisites

Ensure the target virtual host has Rewrite enabled. Path: WebAdmin console > Configuration > Virtual Hosts > [Your Virtual Host] > Rewrite.

Common Rewrite Rule Examples

Add the following rules to the virtual host's Rewrite configuration.

Example 1: Cache PHP Files in a Specific Directory

Cache all .php files in the cacheable/ directory for 120 seconds.

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

Example 2: Enable Private Cache for Logged-in Users

This rule caches specific static file types for logged-in users (determined by cookie) and marks them as private cache.

# Enable private cache for logged-in users
RewriteCond %{REQUEST_METHOD} ^HEAD|GET$
RewriteCond %{HTTP_COOKIE} loginuser
RewriteCond %{ORG_REQ_URI} !^/index.php$
RewriteCond %{ORG_REQ_URI} (.php|.html|.htm|.feed|.pdf|/[^.]*)$  [NC]
RewriteRule .* - [E=cache-control:private,L]

Example 3: Disable Cache for a Specific Virtual Host

Add the following rule to prevent all requests for this virtual host from being cached.

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

Cache Module Parameter Details

The table below details the parameters for the server-level cache module:

Parameter Description Suggested Value
enableCache Enable/disable public cache. 1=Enable, 0=Disable. If both public and private caches are enabled, private cache takes priority. 0 (Enable per virtual host)
qsCache Cache URIs with a query string. 1=Enable, 0=Disable. 1
reqCookieCache Handle requests with cookies. 1=Respond with cache if exists, 0=Never respond with cache for requests with cookies. 1
respCookieCache Cache responses with Set-Cookie headers. 1=Cache, 0=Do not cache. 1
ignoreReqCacheCtrl Ignore cache-control headers in requests. 1=Ignore, 0=Respect. 0
ignoreRespCacheCtrl Ignore cache-control headers in responses. 1=Ignore, 0=Respect. 0
expireInSeconds Expiration time (seconds) for public cache resources. 2000
maxStaleAge Maximum age (seconds) to serve stale cache when a fresh resource is unavailable. 1000
enablePrivateCache Enable/disable private cache. 1=Enable, 0=Disable. 1
privateExpireInSeconds Expiration time (seconds) for private cache resources. 1000
storagePath Cache data storage directory. Starts with / for absolute path, otherwise relative to OpenLiteSpeed root. Leave empty to use the default cachedata directory. (Empty for default)

Important Note: This configuration is based on newer OpenLiteSpeed versions. Before proceeding, confirm your specific version and refer to the latest official documentation for fine-tuning. Virtual host-level Rewrite rules have the highest priority and are the primary method for granular cache control.

Post a Comment

Your email will not be published. Required fields are marked with *.