Speed Up vBulletin Sites Through LiteSpeed Built-in Cache

LiteSpeed has provided a built-in cache functionality (an advanced feature for Enterprise 2+CPU licenses) since version 4.0 (March, 2009), and keeps improving this feature through subsequent 4.0.x releases. Many of you maybe still not aware of the powerfulness of this feature.  This article will show a sample usage for the popular vBulletin  forum with detailed steps, and you are welcome to post your feedback on your real-life exciting results.

LiteSpeed cache has similar features as Apache mod_cache but implemented in a more efficient way, and works like Varnish. It is an output cache for dynamic contents, so the usage does not limit to PHP pages.  Unlike  Varnish, it is built into the web server, reducing one layer of reverse proxy. Hence for static contents it is more efficient without one extra layer.  The uniqueness of LiteSpeed cache is that you can use rewrite rules (either in configuration files or .htaccess) to control its behavior, which gives you maximum flexibility.

Sample Use Case for vBulletin forums (vB 3.8.x or vB 4.0.x)

Here we give one example of how to use LiteSpeed cache with rewrite rules to speed up vBulletin forums. We would like to cache the pages for guest users, which generate the majority of the traffic.

1. Install vBulletin plug-in – Boost Product XML “product-boostv1.xml” from vBulletin admin panel – manage plugin.

You can download from here. It is a good article elaborating about why output cache is essential for high traffic sites.  The purpose of this plug-in is to determine if the user is a guest user or a logged in member. You do not need boost.php, that’s for Varnish only.

2. Enable the cache function. You have to go to the LiteSpeed admin console to set this up, mod_cache config for “CacheRoot” will be ignored.

a. Go to admin console->Server->Cache

b. Cache Storage Settings->Storage Path: set to a fast local disk, for e.g.: /tmp/diskcache (you can also set it to a ramdisk if you have enough extra memory to speed up further)

c. Manually create the directory from shell, and give permission for litespeed/lshttpd processes (assume running as “nobody” here):

#mkdir /tmp/diskcache
#chown nobody:nobody /tmp/diskcache
#chmod 700 /tmp/diskcache

3. Set vhost Cache policy

a. For natively configured vhost (for e.g. myforum) in admin console:

i.  Go to admin console->Virtual Hosts->myforum->Cache

ii.  Disable the cache globally (will be enabled through rewrite rules under condition)

Enable Cache:No

iii. Set cache policy

Cache Request with Query String:Yes
Cache Request with Cookie:Yes
Cache Response with Cookie:Yes
Ignore Request Cache-Control:Yes
Ignore Response Cache-Control:Yes

b. For imported Apache vhost:

There is no need to use mod_cache directive to control cache. You do not need to do anything for the vhost cache policy. It will assume the same default settings as the above natively configured vhost. However, you do need to set the server level Cache storage from the Litespeed web admin console stated in step 2; and put rewrite rules in the .htaccess file as in step 4.

4. Add rewrite rules to cache guest pages for 2 minutes

Assuming vBulletin is installed at $DOCUMENT_ROOT/forum

## following redirect will reduce cached files:
## otherwise domain.com/abc.php and www.domain.com/abc.php will create 2 cached files.
RewriteCond %{HTTP_HOST} !^www\.domain\.com$
RewriteRule /forum/(.*)$ http://www.domain.com/forum/$1 [R=301,L]

## select which pages to cache
RewriteCond %{HTTP_COOKIE} !bbimloggedin=yes
RewriteCond %{HTTP_COOKIE} !bbuserid=
RewriteCond %{HTTP_COOKIE} !bbpassword=
RewriteCond %{REQUEST_METHOD} ^HEAD|GET$
RewriteCond %{QUERY_STRING} !s=[a-fA-F0-9]{32}
RewriteCond %{QUERY_STRING} !product=vbnexus
RewriteCond %{REQUEST_URI} !^/forum/(login|register|usercp|private|profile|cron|image) \.php$
RewriteCond %{REQUEST_URI} !^/forum/admincp
RewriteRule /forum/(.*\.php)?$ – [L,E=Cache-Control:max-age=120]

You can add the rewrite rules in either of the 3 locations:

  • .htaccess  (adding a line at beginning: “RewriteEngine on”)
  • Apache config file vhost section
  • Litespeed natively configured vhost: admin console->Virtual Hosts->myforum->Rewrite

5.  Apply changes by restarting LiteSpeed

6.  Optional:  Set a cron job to clean up cache directory /tmp/diskcache

*/10 * * * * root find /tmp/diskcache -type f -mmin +8 -delete 2>/dev/null

Every 10 minutes, delete cached files that are more than 8 minutes old. Since we set the cache expire time as 120 seconds (2 minutes),  it’s safe to delete those old files.

7. Verify if pages are served from the cache

Since 4.0.19, LiteSpeed will output a response header “X-LiteSpeed-Cache hit” if the content is served from its internal cache.

8. Optional: If you want to log cache hit data for future analytics, you can use the environment variable “CACHE_HIT=1” (introduced in 4.0.19) in your in customized access log with format string “%{CACHE_HIT}e”.

For example: in the httpd.conf vhost section, add

CustomLog /usr/local/apache/domlogs/domamin.com-cache_log “%h %t \”%r\” %>s %b %{CACHE_HIT}e”

Reference:

  1. How does the output cache work? vBulletion forum
  2. Big boost: related posts at our forum.

Tags:

Related Posts


Comments