imagecache and mod_rewrite for the win

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 http://static.example.com, 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:

<Directory /var/www/static.example.com/sites/all/files/imagecache/>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ http://www.example.com/sites/all/files/imagecache/$1 [P]
</Directory>

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 www.example.com 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!

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Rewriting to static.example.com

Very helpfull post, thank you.

I managed to do the redirection of all content in the /files folder by adding the following to my .htaccess in that files folder:

RewriteEngine on

RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_URI} ^/sites/default/files/imagecache.* RewriteRule ^(.*)$ http://imagecache.example.com/index.php?q=sites/default/files/$1 [L]


RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC] RewriteRule ^(.*)$ http://static.example.com/sites/default/files/$1 [L,R=301]

But i would rather have drupal generate the URL from scratch, as that saves a lot of hits against the drupal apache instance. Is it correct that you have to change core for that? More particularly, to change file_create_path() in file.inc ? How have you done this?

Did you try to setup on

Did you try to setup on Nginx?

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options