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:


<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



<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 /var/www/mydomain.com/>
        AllowOverride All

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

    <IfModule mod_disk_cache.c>
        CacheEnable disk /cached/

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

PHP image generation script /var/www/mydomain.com/img/cached/logo.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;


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


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 ;)


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>
        CacheEnable disk  /
        CacheDirLevels 2
        CacheDirLength 1
    CacheMaxFileSize 10000000
    CacheDefaultExpire 86400

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.