LSCache is a built-in page caching feature of the OpenLiteSpeed web server that can significantly improve the speed and performance of your website by storing frequently accessed data in a cache.
In this article, we’ll show you exactly how to use LSCache on RunCloud servers in web applications that don’t already have built-in LSCache support.
Prerequisites
Before you start, you’ll need to have the following:
- A RunCloud account, and a connected server with the OpenLiteSpeed stack.
- A web application installed on your server.
How to Choose Between Public and Private Caching
Caching is a technique that allows the web server to store frequently accessed data in a temporary storage area, called a cache. This can improve the speed and performance of the website by reducing the amount of time it takes for the server to process and retrieve the data when a user requests a page.
What is Public Caching?
Public caching, also known as shared caching, is when the cache is used by more than one client. For example, a reverse proxy or a gateway cache can act as a public cache for multiple users.
Public caching can offer a greater performance gain and a much greater scalability gain, as a user may receive cached copies of web pages without ever having obtained a copy directly from the origin server themselves.
For example, if you have a website that sells books, you might want to use public cache for the pages that show the book details, reviews, or categories. These pages are the same for all users, and so can be served from a public cache.
What is Private Caching?
A private cache, however, is only accessible to an individual visitor. It usually contains information that is only relevant to that particular user, such as the WordPress admin bar, the shopping cart, or their profile page. When a page is privately-cached, there is a separate, personalized copy stored for each user that requests it.
For example, if you have a website that sells books, you might want to use a private cache for the pages that show the user’s order history, wishlist, or recommendations. These pages are different for each user, and so should not be served from a public cache.
When to Use Public Caching, Private Caching, or No Caching
Each particular URL of a website can be set up to be publicly cached, privately cached, or not cached at all, but cannot be both publicly and privatly cached at the same time. That is to say, you can only set up one cache type for a particular URL. Depending on the situation, you might want to set different URLs to be cached differently. The following outlines a few scenarios helpful in determining which type of caching should be used for a URL or a set of URLs.
Use public caching for web pages that:
- Do not change frequently.
- Have high demand (requested frequently).
- Are not sensitive or confidential.
- Do not depend on who is looking at them.
Use private caching for web pages that:
- Can only be used by one user/client, such as personal information on a website (for authorized users).
- Use resources such as documents that are only available for one particular user or authorized users.
- Generates responses with cookies.
Use no caching for web pages that:
- Use a POST request.
- Have dynamic content (such as time sensitive info).
- Change frequently.
- Should not be stored, such as the user’s payment details.
How Cache Settings are Inherited and Overridden in LiteSpeed Servers
Cache settings can be configured at different levels, such as server, virtual host, context, and script handler. The settings at each level affect how the cache works for the web pages under that level.
Cache settings follow a general-to-specific hierarchy, and specific settings override general settings. This means that:
- Cache settings at the server level apply to all web pages on the server, unless they are overridden by lower-level settings.
- Cache settings at the virtual host level apply to all web pages under that virtual host (web application), unless they are overridden by lower-level settings.
- Cache settings at the context level apply to all web pages under that context (location), unless they are overridden by lower-level settings.
For example, if you enable public cache at the server level, but disable it at the virtual host level, then public cache will be disabled for all web pages under that virtual host.
Enabling Caching
To use LSCache on RunCloud servers, you need to do the following steps:
1. Enable the LSCache Module at Server Level
The first step is to enable the LSCache module on your OpenLiteSpeed server. You can do this by logging into the RunCloud dashboard, navigating to the server dashboard, and clicking on the “LiteSPeed” button in the left menu.
Here, you can edit the module cache block and set the following settings at the server level:
module cache {
ls_enabled 1 # Enable LSCache module
storagePath $VH_ROOT/lscache # Set the cache storage path
checkPrivateCache 1 # Enable checking private cache
checkPublicCache 1 # Enable checking public cache
maxCacheObjSize 10000000 # Set the maximum cache object size in bytes
maxStaleAge 200 # Set the maximum stale age in seconds
qsCache 1 # Enable caching URIs with query strings
reqCookieCache 1 # Enable caching requests with cookies
respCookieCache 0 # Disable caching responses with Set-Cookie header
ignoreReqCacheCtrl 0 # Respect the cache control settings in the request
ignoreRespCacheCtrl 0 # Respect the cache control settings in the response
enableCache 1 # Enable public cache
expireInSeconds 3600 # expiration time for public cache in seconds
enablePrivateCache 0 # Disable private cache
privateExpireInSeconds 3600 # expiration time for private cache in seconds
}
You can also adjust other settings according to your preferences and needs, such as expireInSeconds, maxStaleAge, qsCache, etc. You can find more information about these settings in the OpenLiteSpeed documentation.
After editing the module cache block, save the changes and restart the OpenLiteSpeed server.
2. Configure Virtual Host-Level Cache Settings
If you have more than one web application on your server, you may want to customize the cache settings for each virtual host. If you do not configure any cache settings at the virtual host level, then the cache settings at the server level will be inherited.
To configure cache settings at the virtual host level, you need to add the cache module under each virtual host, and edit the OpenLiteSpeed config file to configure settings in the same way that you did at the server level.
Using the .htaccess File
An .htaccess file is a configuration file that allows you to override the server settings for a specific directory or web application. You can use our file manager tool on our panel to create and edit .htaccess files.
- Log in to the RunCloud panel and navigate to the file manager tool
- The next step is to locate the web app root folder of your web application. The web app root folder is the directory where your web application files are stored. For example, if your web application serves content from /public, then that is your web app root folder.
- Click the New File button and enter ‘.htaccess’ as the file name (including the full stop/period at the start of the filename).
- Finally, open the .htaccess file with the file manager tool and add rewrite rules. Rewrite rules are instructions that tell OpenLiteSpeed how to handle requests for certain URLs or conditions. You can use rewrite rules to enable or disable caching for specific URLs or web pages.
To enable cache via .htaccess, you can add this script to the .htaccess file:
<IfModule LiteSpeed>
RewriteEngine On
RewriteRule (.*\.php)?$ - [E=cache-control:max-age=120]
</IfModule>
This will cache all .php files on your web application for 120 seconds.
To enable cache for your entire website except /private URL, you can try something like this:
<IfModule LiteSpeed>
RewriteEngine On
## cache should be available for HEAD or GET requests
RewriteCond %{REQUEST_METHOD} ^HEAD|GET$
# excluding certain URLs
RewriteCond %{REQUEST_URI} !/(private)/$
# cache for 2 mins for php pages only
RewriteRule /(.*\.php)?$ - [E=Cache-Control:max-age=120]
</IfModule>
This will exclude /private URL from caching. You can use this to prevent caching of your admin panel.
To enable private cache for sessions with cookies, you can write the config like this:
<IfModule LiteSpeed>
# for those not met above condition, enable private cache.
RewriteCond %{REQUEST_METHOD} ^HEAD|GET$
# excluding certain URLs
RewriteCond %{REQUEST_URI} !/(private)/$
## select which pages to serve from private cache
RewriteCond %{HTTP_COOKIE} !my-cookie=yes
# private cache for however long set in cache policy for php pages only
RewriteRule (.*\.php)?$ - [E=Cache-Control:max-age=120]
RewriteRule (.*\.php)?$ - [E=Cache-Control:private]
</IfModule>
This will enable private cache for users who have a cookie named my-cookie stored on their computer, such as logged-in users or commenters.
You can also use other rewrite conditions and flags to fine-tune your caching rules, such as checking for query strings, headers, etc. You can find more information about rewrite rules on the OpenLiteSpeed Knowledgebase.
3. Test LSCache
The final step is to test if LSCache is working properly on your web application. You can do this by using online tools such as LSCache Check or browser developer tools to check the response headers of your web pages.
You can also use the following curl command to check the headers of a page.
curl -I https://example.com/
Executing the above command should give an output similar to the following screenshot:
This means that OpenLiteSpeed has served a cached page from LSCache and has added a x-litespeed-cache header with a value of ‘hit’.
Conclusion
In this article, we have covered how to customize the LS Cache settings for each virtual host on OpenLiteSpeed, and how to use directives or rewrite rules in the .htaccess file of your virtual host. You can find more information about OpenLiteSPeed cache configuration on the official site.
We hope this article was helpful and informative. If you have any questions or feedback, please feel free to leave a comment below.
If you are looking for an easy and convenient way to manage your server, you may want to check out RunCloud. RunCloud is a cloud-based server management platform that allows you to deploy, configure, and monitor your web applications on any cloud provider.With RunCloud, you can easily install and update OpenLiteSpeed, LS Cache, and other web server components with just a few clicks. You can also enjoy features such as SSL certificates, backups, firewall, cron jobs, and more. Start using RunCloud today!