Using Imagecache with lighttpd behind apache mod_rewrite rules

deverman - September 11, 2008 - 10:32

I have installed lighttpd as my static file server like suggested by the Lullabots:

http://www.lullabot.com/articles/using-lighttpd-static-file-server

I put the server behind apache using the suggested proxy pass based on this article:

http://www.linux.com/feature/51673

I have specified some static image directories to serve through lighttpd and this works well.

ProxyPass /images http://0.0.0.0:81/images

I am using imagecache module more and more on my site and I haven't found a way to to use the proxy capability properly because if the imagecache version of the file does not exist I want the request to go to apache. If the imagecache version of the file exists then the request should be passed to lighttpd. The Lullabots suggested using their perl script but I think that it would be best to use mod_rewrite in apache with the [P] flag. I have it almost figured out but I need some help on the regular expression:

The REQUEST_URI that we get is this but this is not a valid path because imagecache puts the "files/gallery" before the file name:

/files/imagecache/gallery_thumbnail/files/gallery/filename.jpg

Image cache preset is "gallery_thumbnail". I have set up the imagecache field to upload the info into the "gallery" subfolder of files.

The correct path should look like this:

%{DOCUMENT_ROOT}/files/imagecache/gallery_thumbnail/filename.jpg

(Document root is the full path to the web site directory)

So I need a rewrite condition with a regular expression that creates a valid file name that checks if the file exists. I tried to come up with one but I'm not good with regular expressions so this is what I have so far:

RewriteCond %{DOCUMENT_ROOT}/files/imagecache/$1/$2 ^/files/imagecache/(.*)/(.*\.(png|gif|jpg|jpeg))$ -f [NC]

You can see everything in context below. Does someone have a solution to finish this off?

# pass static files to lighttpd

ProxyRequests Off
ProxyPreserveHost On

RewriteEngine on
RewriteLog "/opt/local/apache2/logs/rewrite.log"
RewriteLogLevel 9

# Pass image requests for drupal imagecache module images to lighttpd only if files exist
# otherwise pass request to apache so that we can generate the image
RewriteCond %{REQUEST_METHOD} ^GET$
RewriteCond %{REQUEST_URI} ^/files/imagecache
RewriteCond %{DOCUMENT_ROOT}/files/imagecache/$1/$2 ^/files/imagecache/(.*)/(.*\.(png|gif|jpg|jpeg))$ -f [NC]
RewriteRule ^(.*)$ http://0.0.0.0:81%{REQUEST_URI} [L,P]

RewriteLogLevel 0
# patch end - 3/19/08 1:39 PM

Rewrite log output

deverman - September 11, 2008 - 10:48

Oh, I also should have posted the results of the rewrite log output to show you that the main problem is my regular expression:

RewriteCond: input='/documentroot/files/imagecache//files/imagecache/gallery_thumbnail/files/gallery/filename.jpg/' pattern='^/files/imagecache/(.*)/(.*\.(png|gif|jpg|jpeg))$' [NC] => not-matched

 
 

Drupal is a registered trademark of Dries Buytaert.