Reverse-Proxy Cache and WPMU
This is our latest article in the WordPress MU Caching section, make sure you checked out the others !
This article is the less versatile of the ones I wrote in this section, because it will only be interesting to you if you if you have (or plan to have) a multi-server architecture. In this case it’s likely you’ve separated HTTP and SQL servers, and if your platform is big enough that you have multiple HTTP servers. Then, you need a way to dispatch users across the different HTTP servers. While you may have a load balancer, which is the most robust solution to do this (and the single point of failure the least likely to fail), another solution is to use a reverse proxy on a front server. This server can handle all incoming requests, and then route them to abackend HTTP server.
There’s plenty of things you can have the reverse proxy do that will decrease the load on your backend servers without endangering too much its stability, like security (IP banning etc.), content compression (gzip), and another very interesting one is caching.
It is one of the biggest flows in wordpress in my opinion that whenever you load a static file (image, music, office document, etc.), it has to go through PHP and then run many SQL queries just to load this static file. This prevents you, if you don’t want to hack the files manipulating uploads, from using a lightweight HTTP server to serve those static files, and this generates unnecessary load on your machines. Using a cache at the reverse proxy level can help solve this problems : basically the first time a static file is accesse, it will get cached on the reverse proxy server, and all subsequent requests to this file will be served directly from the front end machine, without needing php or mysql.
There are many software solutions for reverse proxies, including Apache 2 with mod_proxy, lighttpd, Squid or Nginx (this last doesn’t have the caching option though).
It’s fairly easy to configure apache 2 with mod_proxy, to give an idea here is a sample configuration :
# Cache static files on disk
CacheEnable disk /files
# Cache avatars in memory
CacheEnable mem /wp-content/avatars
Some warnings :
Caching files on the reverse proxy machine may mean lots of disk accesses for this machine, so make sure it has fast disks and/or cache what can be in memory rather than on disk.
Caching can cause problems if for instance you need to remove a blog that is against the law. Its files will stay in cache until purged, so removing them right away might mean flushing your cache completely.
Apache doesn’t have implemented yet a garbage collection for mod_cache. To prevent your cache from getting too big and/or full of useless files run something like this in cron :
30 3 * * * root /usr/sbin/tmpwatch -fud 72 /home/cache/ >/dev/null 2>/dev/null
This will remove from your cache the files that were not accessed in the past 72 hours.
Happy proxying .