3

I have enabled the mod_cache and mod_disk_cache Apache modules, to enable caching of dynamically generated images form a PHP script (based on width and height parameters).

Everything works fine (Apache caches the files) whenever I supply small width and height parameters to my PHP script. However when I supply larger parameters, and as the image size gets bigger (above around 50k) Apache is not caching the response anymore. In that case Apache does create a directory in the /var/cache/apache/mod_disk_cache but the directory is empty (it does not contain the .header and .data files).

I have tried to set the CacheMaxFileSize directive but it did not seem to work (I tried setting it a a large value (2000000) in the disk_cache.conf file but it did not seem to have any effect and setting the directive in the site configuration file disabled all caching - caching of small files stopped working).

Has anybody encountered something similar and knows how to solve it?

Server is running Ubuntu 12.04 with Apache 2.2.22

Here is my config:

/etc/apache2/mods-available/disk_cache.conf

<IfModule mod_disk_cache.c>
# cache cleaning is done by htcacheclean, which can be configured in
# /etc/default/apache2
#
# For further information, see the comments in that file, 
# /usr/share/doc/apache2.2-common/README.Debian, and the htcacheclean(8)
# man page.

    # This path must be the same as the one in /etc/default/apache2

    CacheRoot /var/cache/apache2/mod_disk_cache

    # This will also cache local documents. It usually makes more sense to
    # put this into the configuration for just one virtual host.

    CacheDirLevels 2
    CacheDirLength 1
    CacheIgnoreCacheControl On
    CacheIgnoreURLSessionIdentifiers jsessionid
    CacheIgnoreURLSessionIdentifiers PHPSESSID
    CacheIgnoreHeaders Set-Cookie
    CacheMaxFileSize 2000000

</IfModule>

/etc/apache2/sites-available/img.mydomain.com

<VirtualHost *:80>
    ServerName img.mydomain.com 
    ServerAlias img.mydomain.eu
    DocumentRoot /var/www/mydomain.com/img/

    <Directory />
        Options None
        AllowOverride None
        Order allow,deny
        Deny from all
    </Directory>

    <Directory /var/www/mydomain.com/>
        AllowOverride All
    </Directory>

    <Directory /var/www/mydomain.com/img/>
        Options FollowSymlinks
        Order allow,deny
        Allow from all
    </Directory>

    <IfModule mod_disk_cache.c>
        CacheEnable disk /cached/
    </IfModule>

    ErrorLog ${APACHE_LOG_DIR}/img.mydomain.com-error.log
    LogLevel debug
    CustomLog ${APACHE_LOG_DIR}/img.mydomain.com-access.log combined
</VirtualHost>

PHP image generation script /var/www/mydomain.com/img/cached/logo.php

<?php

//Generate Imagick image based on provided width and height

header("Content-Type: image/" . strtolower($image->getImageFormat()));
header("Content-Length: " . strlen($image));
header("Cache-Control: public, must-revalidate, max-age=2592000");
echo $image;

?>
quentinadam
  • 321
  • 2
  • 12

2 Answers2

1

The way a friend of mine has found is to force caching of whole file by using wget, simply:

wget http://my.domain.com/mybigfile.ogg

or

wget -O /dev/null http://my.domain.com/mybigfile.ogg

without Range!

This will force apache to cache the whole file.

Than after that, accessing range in cached files is now possible...

( Sorry for the late answer: I've found this question by searching for this kind of solution. So after looking the running workaround, I post this ;)

0

you need to tell it the max file size it stores, the default is 1MB so each file larger than than wont get cached

set the max size to something like 10MB in the config for the disk_cach

CacheMaxFileSize 10000000

here is a configuration file as an example: LoadModule cache_module modules/mod_cache.so

<IfModule mod_cache.c>
    LoadModule cache_disk_module modules/mod_cache_disk.so
    <IfModule mod_cache_disk.c>
        CacheRoot DIRECTORY_OF_THE_DISK_CACHE_HERE
        CacheEnable disk  /
        CacheDirLevels 2
        CacheDirLength 1
    CacheMaxFileSize 10000000
    CacheDefaultExpire 86400
    </IfModule>
</IfModule>