imagecache and mod_rewrite for the win


We had to setup a static file server for a client site - its growing too big for a single box running php, mysql and serving static content. So, we have a shiny new static server that mounts the files directory of the php server via NFS. However, the site uses the ridiculously useful Imagecache module, which requires that Drupal intercept image requests for non-existent images and create them on the fly.

But, we don't want any image requests to hit Drupal. Instead, we want all requests for images to go to, which is a tuned apache running the threaded worker MPM with no PHP or Drupal in site.

The solution? apache's mod_rewrite and mod_proxy.

First, turn on mod_proxy and mod_rewrite (note, if you're not using Debian or a Debian-based distribution, that's not my fault): static:~# a2enmod proxy static:~# a2enmod proxy_http static:~# a2enmod rewrite static:~# /etc/init.d/apache2 restart then, add this to your apache configuration for the imagecache directory: RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$$1 [P]

This tells mod_rewrite to send a reverse-proxy request to your php site if it can't find a file. This is the secret sauce, as it will cause your imagecache setup on to generate the derived image as usual so that the next time that image is requested from the static server, it will be there, and the RewriteCond %{REQUEST_FILENAME} !-f condition will fail, and the file will be served directly by the static server.

mod_rewrite and mod_proxy for the win!

Alex Urevick-Ackelsberg
Alex Urevick-Ackelsberg

Co-Founder / CEO

With nearly two decades of professional technology experience, Alex oversees all business functions at Zivtech and, from sales and marketing, to client relations, human resources, and business strategy.

Related Services

Ready to get started?

Tell us about your project