Archive - Web hosting RSS Feed

WP Super Cache with Nginx

WP Super Cache is a very good tool for speeding up your WordPress installation while at the same time reducing server load. However, WP Super Cache uses mod_rewrite to handle requests, which is not compatible with Nginx. For it to work with Nginx we need to create a few rewrite rules in the server config file.

In my example I have Nginx installed with php5 running as FastCGI, configured like this.

This requires permalinks to be used, request containing query strings will not be served cached content. The following rules are added to the location / { } block in the Nginx config file for that virtual host. This assumes WordPress is not installed in a sub folder.

# Return existing files
if (-f $request_filename) {
        break;
}

set $supercache_file '';
set $supercache_uri $request_uri;

if ($request_method = POST) {
        set $supercache_uri '';
}

# Bypass cache for requests containing a query string
if ($query_string) {
        set $supercache_uri '';
}

if ($http_cookie ~* "comment_author_|wordpress|wp-postpass_" ) {
        set $supercache_uri '';
}

# Specify the cache file
if ($supercache_uri ~ ^(.+)$) {
        set $supercache_file /wp-content/cache/supercache/$http_host/$1index.html;
}

# Serve the cache file, if it exists
if (-f $document_root$supercache_file) {
        rewrite ^(.*)$ $supercache_file break;
}

# Everything else goes to index.php
if (!-e $request_filename) {
        rewrite . /index.php last;
}

Comparison of APC and XCache

APC
Alternative PHP Cache, will be included in the core of PHP 6.

XCache
Developed by one of the developers of Lighttpd.

Benchmarks where done on a small Amazon EC2 instance, running official Ubuntu 9.10 image. Benchmarks where done on two different occasions, average results are used. The following packages are installed from APT.

  • libapache2-mod-php5
  • php5-mysql
  • php-apc
  • php5-xcache
  • mysql-server

The following web sites will be tested

  • Hello World script
  • WordPress 2.9.1
  • Joomla 1.5.15

Benchmarking itself is done with ab (Apache benchmarking tool) on localhost, doing 1000 requests over 10 connections (-n 1000 -c 10). Interesting numbers are request per second.

Here’s the results, numbers are requests per second, the percent is the improvement.

Without accelerator With APC With XCache
Hello World 1499 1602 (7%) 1563 (4%)
WordPress 2,82 12,61 (347%) 12,97 (360%)
Joomla 2,25 3,60 (60%) 3,48 (55%)

The big increase in WordPress performance could be because not much sample data is installed in the default installation (compared to the option in the Joomla install, which i used), which could mean that the large bottleneck in Joomla is MySQL not PHP, while being the other way around for WordPress. But even with large amount of data, the performance improvement should be good.

Conclusion
Since there isn’t any big difference in performance between the two, I would choose APC. Mainly because it will be included in the core of PHP 6.

The problem with Apache

Apache is a great web server, I use it myself a lot. There is however one huge problem when working with it in big deployments, and that’s the enormous start and reload times when working with many virtual hosts. When using regular web servers you probably don’t have more then 2 or 3 thousand vhosts, but when deploying a cluster of load balanced web servers you can easily handle tens of thousand of them. And that’s where the problem come in.

This very nice excel graph shows the problem quite nicely.

With 2000 vhosts there’s no problem, the start time is around 2 seconds and reload time is around 5 seconds. But with 40 000 of them it’s another story, as you can see.

These tests where performed on a dual xeon server with 2GB of memory and a nice (15krpm, RAID10) SAN backend for storage and no load on the server.

In comparison, Litespeed reloads with 40 000 vhosts in under 5 seconds.

Page 2 of 2«12